From 6e00c950b67eba1c81a737ab39ababa4549db9a5 Mon Sep 17 00:00:00 2001 From: Dan Hromada Date: Mon, 12 May 2014 11:22:21 +0200 Subject: [PATCH] Apply saved scripts --- lib/popup.js | 61 +++++++++++++++++++++---- lib/script.js | 121 ++++++++++++++++++++++++++++++-------------------- manifest.json | 2 +- 3 files changed, 127 insertions(+), 57 deletions(-) diff --git a/lib/popup.js b/lib/popup.js index 9bcce0b..a606a90 100644 --- a/lib/popup.js +++ b/lib/popup.js @@ -22,13 +22,25 @@ goTo: "Jump to the selected host" }, share: "Share Your script with other people", - save: "Save and apply this script", + save: "Save and apply this script", include: { textarea: 'Uncomment address of script below or type your own (one per line)', mask: 'Click to close textarea popup' }, draft: "This is a draft, click to remove it" }, + applyTitles: function() { + this.el.hostSelect.attr('title', this.title.host.select); + this.el.hostGoToLink.attr('title', this.title.host.goTo); + + this.el.includeTextarea.attr('title', this.title.include.textarea); + this.el.includeMask.attr('title', this.title.include.mask); + + this.el.shareBtn.attr('title', this.title.share); + this.el.saveBtn.attr('title', this.title.save); + this.el.shareBtn.attr('title', this.title.share); + this.el.draftRemoveLink.attr('title', this.title.draft); + }, include: { predefined: [ { @@ -89,8 +101,11 @@ apiclb: { onSelectedTab: function(tab) { var handlers = popup.apiclb; - chrome.tabs.sendRequest(tab.id, {method: "getHost", reload: false}, handlers.onGetHost); - chrome.tabs.sendRequest(tab.id, {method: "getCustomJS", reload: false}, handlers.onGetScript); + + popup.tabId = tab.id; + + chrome.tabs.sendRequest(popup.tabId, {method: "getHost", reload: false}, handlers.onGetHost); + chrome.tabs.sendRequest(popup.tabId, {method: "getCustomJS", reload: false}, handlers.onGetScript); }, onGetHost: function(response) { if( !response || typeof response.host !== 'string' ) { @@ -120,8 +135,8 @@ if( data.source ) { // source is encoded as base64 if( data.source.indexOf('data:text/javascript;base64,') === 0 ) { - data.source.replace('data:text/javascript;base64,', ''); - data.source = atob(data.source); + data.source = data.source.replace('data:text/javascript;base64,', ''); + popup.data.source = atob(data.source); } else { popup.data.source = data.source; @@ -139,7 +154,7 @@ } // Apply draft if exist - popup.applyData(popup.storage.get('draft')); + popup.applyData(popup.storage.get('draft')); } }, data: { @@ -187,7 +202,7 @@ popup.el.enableCheck.prop('checked', data.config.enable); // Fill 'extra include' textarea - popup.el.includeTextarea.val(data.extra); + popup.el.includeTextarea.val(data.config.extra); // Apply source into editor popup.editor.apply(data.source); @@ -207,6 +222,21 @@ popup.applyData(); popup.el.draftRemoveLink.addClass('is-hidden'); }, + save: function(e) { + var data = popup.getCurrentData(); + + e.preventDefault(); + + data.config.extra = data.config.extra.replace("\n", ';'); + data.source = 'data:text/javascript;base64,' + btoa(data.source); + + chrome.tabs.sendRequest(popup.tabId, {method: "setCustomJS", customjs: data, reload: true}); + + popup.removeDraft(); + window.close(); + + return false; + }, error: function() { alert('err'); } @@ -214,6 +244,12 @@ window.popup = popup; + /** + * Add titles to elements + */ + popup.applyTitles(); + + /** * Fill predefined libs to include */ @@ -258,16 +294,25 @@ setInterval(function() { var draft = popup.getCurrentData(), source = draft.source.replace(popup.editor.defaultValue, ''); - console.log(popup.data.source); + if( (source || !popup.data.source) && source !== popup.data.source ) { popup.storage.set('draft', draft); + // Highlight save button popup.el.saveBtn.addClass('pure-button-primary') + // Auto switch 'enable checkbox' on source edit + popup.el.enableCheck.prop('checked', true); } else { + // No changes or no source - remove save button highlight popup.el.saveBtn.removeClass('pure-button-primary') } }, 1000); + /** + * Save script + */ + popup.el.popupForm.on('submit', popup.save); + /** * Remove draft diff --git a/lib/script.js b/lib/script.js index 0feea57..0ca3f6a 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,54 +1,79 @@ -var customjs = localStorage['customjs']; -if( customjs ) { - try { - customjs = JSON.parse(customjs); +(function() { + function injectScript(src, where) { + var elm = document.createElement('script'); + elm.src = src; + document[where || 'head'].appendChild(elm); } - catch(e) { // Old version - customjs = { - src: customjs, - config: { - enable: true, - include: '' + + var customjs = localStorage['customjs']; + if( customjs ) { + try { + customjs = JSON.parse(customjs); + } + catch(e) { + // Backward compatibility (version >1) + customjs = { + source: customjs, + config: { + enable: true, + include: '', + extra: '' + } + }; + localStorage['customjs'] = JSON.stringify(customjs); + } + + if( customjs.config.enable ) { + + // Predefined include + if( customjs.config.include ) { + injectScript('https://ajax.googleapis.com/ajax/libs' + customjs.config.include); } - }; - localStorage['customjs'] = JSON.stringify(customjs); - } - - if( customjs.config.enable ) { - if( customjs.config.include ) { - var jquery = document.createElement('script'); - jquery.src = 'https://ajax.googleapis.com/ajax/libs' + customjs.config.include; - document.head.appendChild(jquery); - } - if( customjs.src ) { - var cusomscript = document.createElement('script'); - cusomscript.src = 'data:text/javascript,' + decodeURI(customjs.src); - setTimeout(function() { - document.body.appendChild(cusomscript); - }, 250); + + // Extra include + var extra = (customjs.config.extra || '').split(';'); + extra.forEach(function(line) { + if( line.substr(0, 1) !== '#' ) { + injectScript(line); + } + }); + + // Script + if( customjs.source || customjs.src ) { + setTimeout(function() { + // Backward compatibility (version 1) + if( customjs.src ) { + customjs.source = 'data:text/javascript;' + customjs.src; + } + console.log(customjs); + injectScript(customjs.source, 'body'); + }, 250); + } + } } -} -chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { - switch(request.method) { - case 'getHost': - sendResponse({host: location.host}); - break; - case 'setCustomJS': - localStorage['customjs'] = JSON.stringify(request.customjs); - case 'getCustomJS': - var customjs = JSON.parse(localStorage['customjs'] || '{}'); - sendResponse({customjs: customjs}); - break; - case 'removeCustomJS': - delete localStorage['customjs']; - break; - default: - sendResponse({src: '', config: {}}); - } - if( request.reload ) { - window.location.reload(); - } -}); \ No newline at end of file + chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { + switch(request.method) { + case 'getHost': + sendResponse({host: location.host}); + break; + case 'setCustomJS': + localStorage['customjs'] = JSON.stringify(request.customjs); + case 'getCustomJS': + var customjs = JSON.parse(localStorage['customjs'] || '{}'); + sendResponse({customjs: customjs}); + break; + case 'removeCustomJS': + delete localStorage['customjs']; + break; + default: + sendResponse({src: '', config: {}}); + } + if( request.reload ) { + window.location.reload(); + } + }); + +})(); diff --git a/manifest.json b/manifest.json index 126e93f..06092ae 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "name": "Custom JavaScript for websites ", "description": "Run custom JavaScript on any website.", - "version": "1.1.3", + "version": "2.0.0", "permissions": [ "http://*/",