Apply saved scripts
This commit is contained in:
parent
9daa4a3f09
commit
6e00c950b6
3 changed files with 127 additions and 57 deletions
57
lib/popup.js
57
lib/popup.js
|
@ -29,6 +29,18 @@
|
|||
},
|
||||
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;
|
||||
|
@ -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
|
||||
|
|
|
@ -1,32 +1,55 @@
|
|||
(function() {
|
||||
function injectScript(src, where) {
|
||||
var elm = document.createElement('script');
|
||||
elm.src = src;
|
||||
document[where || 'head'].appendChild(elm);
|
||||
}
|
||||
|
||||
var customjs = localStorage['customjs'];
|
||||
if( customjs ) {
|
||||
try {
|
||||
customjs = JSON.parse(customjs);
|
||||
}
|
||||
catch(e) { // Old version
|
||||
catch(e) {
|
||||
// Backward compatibility (version >1)
|
||||
customjs = {
|
||||
src: customjs,
|
||||
source: customjs,
|
||||
config: {
|
||||
enable: true,
|
||||
include: ''
|
||||
include: '',
|
||||
extra: ''
|
||||
}
|
||||
};
|
||||
localStorage['customjs'] = JSON.stringify(customjs);
|
||||
}
|
||||
|
||||
if( customjs.config.enable ) {
|
||||
|
||||
// Predefined include
|
||||
if( customjs.config.include ) {
|
||||
var jquery = document.createElement('script');
|
||||
jquery.src = 'https://ajax.googleapis.com/ajax/libs' + customjs.config.include;
|
||||
document.head.appendChild(jquery);
|
||||
injectScript('https://ajax.googleapis.com/ajax/libs' + customjs.config.include);
|
||||
}
|
||||
if( customjs.src ) {
|
||||
var cusomscript = document.createElement('script');
|
||||
cusomscript.src = 'data:text/javascript,' + decodeURI(customjs.src);
|
||||
|
||||
// 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() {
|
||||
document.body.appendChild(cusomscript);
|
||||
// Backward compatibility (version 1)
|
||||
if( customjs.src ) {
|
||||
customjs.source = 'data:text/javascript;' + customjs.src;
|
||||
}
|
||||
console.log(customjs);
|
||||
injectScript(customjs.source, 'body');
|
||||
}, 250);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,3 +75,5 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
|
|||
window.location.reload();
|
||||
}
|
||||
});
|
||||
|
||||
})();
|
||||
|
|
|
@ -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://*/",
|
||||
|
|
Reference in a new issue