2017-08-25 19:44:03 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
var popup = {
|
|
|
|
key: 'popup',
|
|
|
|
el: {
|
2017-08-25 20:39:10 +02:00
|
|
|
popup: document.getElementById("customjs"),
|
|
|
|
popupForm: document.getElementById("popup-form"),
|
|
|
|
hostSelect: document.getElementById("host"),
|
|
|
|
hostGoToLink: document.getElementById("goto-host"),
|
|
|
|
enableCheck: document.getElementById("enable"),
|
|
|
|
sourceEditor: document.getElementById("ace-editor"),
|
|
|
|
saveBtn: document.getElementById("save"),
|
|
|
|
resetBtn: document.getElementById("reset"),
|
|
|
|
draftRemoveLink: document.getElementById("draft-remove"),
|
|
|
|
error: document.getElementById("error")
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
title: {
|
|
|
|
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() {
|
2017-08-25 20:39:10 +02:00
|
|
|
this.el.hostSelect.setAttribute('title', chrome.i18n.getMessage("select_host_title"));
|
|
|
|
this.el.hostGoToLink.setAttribute('title', chrome.i18n.getMessage("select_host_goto"));
|
2015-04-15 21:58:54 +02:00
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
this.el.saveBtn.setAttribute('title', this.title.save);
|
|
|
|
this.el.draftRemoveLink.setAttribute('title', this.title.draft);
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
host: undefined,
|
|
|
|
emptyDataPattern: {
|
|
|
|
config: {
|
|
|
|
enable: false,
|
|
|
|
extra: ''
|
|
|
|
},
|
|
|
|
source: ''
|
|
|
|
},
|
|
|
|
data: null,
|
|
|
|
editor: {
|
2017-08-25 19:44:03 +02:00
|
|
|
editorInstance: null,
|
|
|
|
defaultValue: chrome.i18n.getMessage("placeholder_javascript"),
|
2015-04-15 21:58:54 +02:00
|
|
|
value: '',
|
|
|
|
init: function() {
|
2017-08-25 20:39:10 +02:00
|
|
|
var editor = this.editorInstance = ace.edit(popup.el.sourceEditor);
|
2015-04-15 21:58:54 +02:00
|
|
|
editor.setTheme("ace/theme/tomorrow");
|
|
|
|
editor.getSession().setMode("ace/mode/javascript");
|
2017-08-25 19:44:03 +02:00
|
|
|
// editor.setHighlightActiveLine(false);
|
2015-04-15 21:58:54 +02:00
|
|
|
editor.getSession().on('change', this.onChange);
|
2017-08-25 19:44:03 +02:00
|
|
|
editor.$blockScrolling = Infinity;
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
apply: function(source) {
|
2017-08-25 19:44:03 +02:00
|
|
|
var editor = this.editorInstance;
|
2015-04-15 21:58:54 +02:00
|
|
|
editor.setValue(source);
|
|
|
|
editor.gotoLine(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
storage: {
|
|
|
|
data: {
|
|
|
|
private: {},
|
|
|
|
global: {}
|
|
|
|
},
|
|
|
|
MODE: {
|
|
|
|
private: 1,
|
2017-08-25 19:44:03 +02:00
|
|
|
global: 2
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
setMode: function(mode) {
|
2017-08-25 19:44:03 +02:00
|
|
|
if (mode === this.MODE.private) {
|
2015-04-15 21:58:54 +02:00
|
|
|
this.key = popup.key + "-" + popup.protocol + "//" + popup.host;
|
|
|
|
this.mode = this.MODE.private;
|
|
|
|
}
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
if (mode === this.MODE.global) {
|
2015-04-15 21:58:54 +02:00
|
|
|
this.key = popup.key;
|
|
|
|
this.mode = this.MODE.global;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
load: function() {
|
|
|
|
this.setMode(this.MODE.private);
|
|
|
|
this._setData(JSON.parse(localStorage.getItem(this.key) || "{}"));
|
|
|
|
|
|
|
|
this.setMode(this.MODE.global);
|
|
|
|
this._setData(JSON.parse(localStorage.getItem(this.key) || "{}"));
|
|
|
|
},
|
|
|
|
_getData: function(key) {
|
|
|
|
var storage = popup.storage;
|
2017-08-25 19:44:03 +02:00
|
|
|
if (storage.mode === storage.MODE.private) {
|
|
|
|
if (key) {
|
2015-04-15 21:58:54 +02:00
|
|
|
return storage.data.private[key];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return storage.data.private;
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 19:44:03 +02:00
|
|
|
if (storage.mode === storage.MODE.global) {
|
|
|
|
if (key) {
|
2015-04-15 21:58:54 +02:00
|
|
|
return storage.data.global[key];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return storage.data.global;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_setData: function(data, key) {
|
|
|
|
var storage = popup.storage;
|
2017-08-25 19:44:03 +02:00
|
|
|
if (storage.mode === storage.MODE.private) {
|
|
|
|
if (key) {
|
2015-04-15 21:58:54 +02:00
|
|
|
storage.data.private[key] = data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
storage.data.private = data;
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 19:44:03 +02:00
|
|
|
if (storage.mode === storage.MODE.global) {
|
|
|
|
if (key) {
|
2015-04-15 21:58:54 +02:00
|
|
|
storage.data.global[key] = data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
storage.data.global = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
get: function(key) {
|
|
|
|
return this._getData(key);
|
|
|
|
},
|
|
|
|
set: function(arg1, arg2) {
|
|
|
|
// arg1 is a key
|
2017-08-25 19:44:03 +02:00
|
|
|
if (typeof arg1 === 'string') {
|
2015-04-15 21:58:54 +02:00
|
|
|
this._setData(arg2, arg1);
|
|
|
|
}
|
|
|
|
// arg1 is data
|
|
|
|
else {
|
|
|
|
this._setData(arg1);
|
|
|
|
}
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
var str = JSON.stringify(this._getData() || {});
|
|
|
|
localStorage.setItem(this.key, str);
|
|
|
|
},
|
|
|
|
remove: function(key) {
|
2017-08-25 19:44:03 +02:00
|
|
|
if (key) {
|
2015-04-15 21:58:54 +02:00
|
|
|
var data = this._getData();
|
|
|
|
delete data[key];
|
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
if (Object.keys(data).length === 0) {
|
2015-04-15 21:58:54 +02:00
|
|
|
this.remove();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var str = JSON.stringify(this._getData());
|
|
|
|
localStorage.setItem(this.key, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
localStorage.removeItem(this.key);
|
|
|
|
this._setData({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
apiclb: {
|
|
|
|
onSelectedTab: function(tab) {
|
|
|
|
popup.tabId = tab.id;
|
|
|
|
chrome.tabs.sendRequest(popup.tabId, {method: "getData", reload: false}, popup.apiclb.onGetData);
|
|
|
|
},
|
|
|
|
onGetData: function(response) {
|
2017-08-25 19:44:03 +02:00
|
|
|
if (!response || typeof response.host !== 'string') {
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.error();
|
|
|
|
return;
|
|
|
|
}
|
2014-05-12 13:20:19 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Create 'hosts select'
|
|
|
|
*/
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.host = response.host;
|
|
|
|
popup.protocol = response.protocol;
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Load storage (global, local) IMPORTANT: Must be called first of all storage operations
|
|
|
|
popup.storage.load();
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Set storage to store data accessible from all hosts
|
|
|
|
popup.storage.setMode(popup.storage.MODE.global);
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
var hosts = popup.storage.get('hosts') || [],
|
|
|
|
url = popup.protocol + "//" + response.host;
|
2014-05-14 09:38:59 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Add current host to list
|
2017-08-25 19:44:03 +02:00
|
|
|
if (hosts.indexOf(url) === -1) {
|
2015-04-15 21:58:54 +02:00
|
|
|
hosts.push(url);
|
|
|
|
}
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Fill 'hosts select'
|
|
|
|
hosts.forEach(function(host) {
|
2017-08-25 19:44:03 +02:00
|
|
|
var option = document.createElement('option');
|
|
|
|
option.innerText = host;
|
|
|
|
if (host === url) {
|
|
|
|
option.setAttribute('selected', 'selected');
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostSelect.appendChild(option);
|
2015-04-15 21:58:54 +02:00
|
|
|
});
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
// Store host (current included in array) if customjs is defined
|
|
|
|
if (response.customjs) {
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.storage.set('hosts', hosts);
|
|
|
|
}
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Set-up data (script, enable, include, extra)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Set-up data pattern if empty
|
2017-08-25 19:44:03 +02:00
|
|
|
if (!popup.data) {
|
|
|
|
popup.data = Object.assign(true, {}, popup.emptyDataPattern);
|
2014-05-11 19:19:09 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Merge host's data to defaults
|
2017-08-25 19:44:03 +02:00
|
|
|
popup.data = Object.assign(popup.data, response.customjs);
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// ... source is now encoded as base64
|
2017-08-25 19:44:03 +02:00
|
|
|
if (popup.data.source.indexOf('data:text/javascript;base64,') === 0) {
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.data.source = popup.data.source.replace('data:text/javascript;base64,', '');
|
|
|
|
popup.data.source = atob(popup.data.source);
|
|
|
|
}
|
2017-08-25 19:44:03 +02:00
|
|
|
else if (popup.data.source.indexOf('data:text/javascript;charset=utf-8,') === 0) {
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.data.source = popup.data.source.replace('data:text/javascript;charset=utf-8,', '');
|
|
|
|
popup.data.source = decodeURIComponent(popup.data.source);
|
|
|
|
}
|
2014-05-12 13:20:19 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Set storage to store data accessible ONLY from current host
|
|
|
|
popup.storage.setMode(popup.storage.MODE.private);
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Save local copy of live data
|
2017-08-25 19:44:03 +02:00
|
|
|
if (response.customjs) {
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.storage.set('data', popup.data);
|
|
|
|
}
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Apply data (draft if exist)
|
|
|
|
popup.applyData(popup.storage.get('draft'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
generateScriptDataUrl: function(script) {
|
|
|
|
var b64 = 'data:text/javascript';
|
|
|
|
// base64 may be smaller, but does not handle unicode characters
|
|
|
|
// attempt base64 first, fall back to escaped text
|
|
|
|
try {
|
|
|
|
b64 += (';base64,' + btoa(script));
|
|
|
|
}
|
2017-08-25 19:44:03 +02:00
|
|
|
catch (e) {
|
2015-04-15 21:58:54 +02:00
|
|
|
b64 += (';charset=utf-8,' + encodeURIComponent(script));
|
|
|
|
}
|
|
|
|
|
|
|
|
return b64;
|
|
|
|
},
|
|
|
|
applyData: function(data, notDraft) {
|
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
if (data && !notDraft) {
|
2017-08-25 20:39:10 +02:00
|
|
|
this.el.draftRemoveLink.classList.remove('is-hidden');
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data = data || this.data;
|
|
|
|
// Default value for source
|
2017-08-25 19:44:03 +02:00
|
|
|
if (!data.source) {
|
2015-04-15 21:58:54 +02:00
|
|
|
data.source = popup.editor.defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set enable checkbox
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.enableCheck.checked = data.config.enable;
|
2015-04-15 21:58:54 +02:00
|
|
|
|
|
|
|
// Apply source into editor
|
|
|
|
popup.editor.apply(data.source);
|
|
|
|
},
|
|
|
|
getCurrentData: function() {
|
|
|
|
return {
|
|
|
|
config: {
|
2017-08-25 20:39:10 +02:00
|
|
|
enable: popup.el.enableCheck.checked
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
2017-08-25 19:44:03 +02:00
|
|
|
source: popup.editor.editorInstance.getValue()
|
2015-04-15 21:58:54 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
removeDraft: function() {
|
|
|
|
popup.storage.setMode(popup.storage.MODE.private);
|
|
|
|
popup.storage.remove('draft');
|
|
|
|
|
|
|
|
popup.applyData();
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.draftRemoveLink.classList.add('is-hidden');
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
save: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Is allowed to save?
|
2017-08-25 20:39:10 +02:00
|
|
|
if (popup.el.saveBtn.classList.contains('pure-button-disabled')) {
|
2015-04-15 21:58:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = popup.getCurrentData();
|
|
|
|
|
|
|
|
// Transform source for correct apply
|
|
|
|
data.source = popup.generateScriptDataUrl(data.source);
|
|
|
|
|
|
|
|
// Send new data to apply
|
|
|
|
chrome.tabs.sendRequest(popup.tabId, {method: "setData", customjs: data, reload: true});
|
|
|
|
|
|
|
|
// Save local copy of data
|
|
|
|
popup.storage.setMode(popup.storage.MODE.private);
|
|
|
|
popup.storage.set('data', popup.data);
|
|
|
|
|
|
|
|
// Clear draft
|
|
|
|
popup.removeDraft();
|
|
|
|
|
|
|
|
// Close popup
|
2017-08-25 20:39:10 +02:00
|
|
|
window.close();
|
2015-04-15 21:58:54 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
reset: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Is allowed to reset?
|
2017-08-25 20:39:10 +02:00
|
|
|
if (popup.el.resetBtn.classList.contains('pure-button-disabled')) {
|
2015-04-15 21:58:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
if (confirm(chrome.i18n.getMessage("delete_warning"))) {
|
2015-04-15 21:58:54 +02:00
|
|
|
// Remove stored data for current host
|
|
|
|
popup.storage.setMode(popup.storage.MODE.private);
|
|
|
|
popup.storage.remove();
|
|
|
|
|
|
|
|
// Remove host from hosts inside global storage
|
|
|
|
popup.storage.setMode(popup.storage.MODE.global);
|
|
|
|
var oldHosts = popup.storage.get('hosts'),
|
|
|
|
newHosts = [];
|
|
|
|
oldHosts.forEach(function(host) {
|
2017-08-25 19:44:03 +02:00
|
|
|
if (host !== popup.protocol + '//' + popup.host) {
|
2015-04-15 21:58:54 +02:00
|
|
|
newHosts.push(host);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popup.storage.set('hosts', newHosts);
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Remove customjs from frontend
|
|
|
|
chrome.tabs.sendRequest(popup.tabId, {method: "removeData", reload: false});
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Set-up empty data
|
2017-08-25 19:44:03 +02:00
|
|
|
popup.data = Object.assign(true, {}, popup.emptyDataPattern);
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.applyData();
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.removeDraft();
|
|
|
|
}
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
error: function() {
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.popup.classList.add('customjs--error');
|
|
|
|
popup.el.error.classList.remove('is-hidden');
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
|
|
|
};
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
window.popup = popup;
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Add titles to elements
|
|
|
|
*/
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.applyTitles();
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
2017-08-25 20:39:10 +02:00
|
|
|
* Click to goTo host link //TODO: JQUERY
|
2015-04-15 21:58:54 +02:00
|
|
|
*/
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostGoToLink.addEventListener('click', function() {
|
2015-04-15 21:58:54 +02:00
|
|
|
var link = popup.el.hostSelect.val();
|
|
|
|
chrome.tabs.sendRequest(popup.tabId, {method: "goTo", link: link, reload: false});
|
|
|
|
window.close();
|
|
|
|
});
|
2014-05-07 20:31:25 +02:00
|
|
|
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Inicialize Ace Editor
|
|
|
|
*/
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.editor.init();
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2014-05-14 09:38:59 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Connect front end (load info about current site)
|
|
|
|
*/
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
chrome.tabs.getSelected(null, popup.apiclb.onSelectedTab);
|
2014-05-14 09:38:59 +02:00
|
|
|
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Auto save draft
|
|
|
|
*/
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
var draftAutoSave = function() {
|
2017-08-25 19:44:03 +02:00
|
|
|
var draft = popup.getCurrentData(),
|
|
|
|
source = draft.source;
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
if ((source || !popup.data.source) && source !== popup.data.source) {
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
popup.storage.setMode(popup.storage.MODE.private);
|
|
|
|
popup.storage.set('draft', draft);
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
// Auto switch 'enable checkbox' on source edit
|
2017-08-25 20:39:10 +02:00
|
|
|
if (!popup.el.enableCheck.classList.contains('not-auto-change')) {
|
|
|
|
popup.el.enableCheck.checked = true;
|
2017-08-25 19:44:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-04-15 21:58:54 +02:00
|
|
|
draftAutoSaveInterval = setInterval(draftAutoSave, 2000);
|
2014-05-12 16:47:50 +02:00
|
|
|
|
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Change host by select
|
|
|
|
*/
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostSelect.addEventListener('change', function(e) {
|
|
|
|
var host = this.value,
|
2017-08-25 19:44:03 +02:00
|
|
|
hostData = JSON.parse(localStorage.getItem(popup.key + '-' + host), true);
|
|
|
|
|
2014-05-12 16:01:21 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
if (host !== popup.protocol + '//' + popup.host) {
|
2015-04-15 21:58:54 +02:00
|
|
|
// Stop making drafts
|
|
|
|
clearInterval(draftAutoSaveInterval);
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Show goto link
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostGoToLink.classList.remove('is-hidden');
|
2014-05-12 11:22:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Hide controls
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.saveBtn.classList.add('pure-button-disabled');
|
|
|
|
popup.el.resetBtn.classList.add('pure-button-disabled');
|
|
|
|
popup.el.draftRemoveLink.classList.add('is-hidden');
|
2014-05-12 16:01:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Apply other host data
|
|
|
|
try {
|
|
|
|
popup.applyData(hostData.data, true);
|
|
|
|
}
|
2017-08-25 19:44:03 +02:00
|
|
|
// Hotfix for host without customjs
|
|
|
|
catch (err) {
|
|
|
|
popup.applyData(Object.assign(true, {}, popup.emptyDataPattern), true);
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Start making drafts
|
|
|
|
draftAutoSaveInterval = setInterval(draftAutoSave, 2000);
|
2014-05-12 16:01:21 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Hide goto link
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostGoToLink.classList.add('is-hidden');
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Show controls
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.saveBtn.classList.remove('pure-button-disabled');
|
|
|
|
popup.el.resetBtn.classList.remove('pure-button-disabled');
|
2017-08-25 19:44:03 +02:00
|
|
|
if (popup.storage.get('draft')) {
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.draftRemoveLink.classList.remove('is-hidden');
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Apply current host data
|
2017-08-25 19:44:03 +02:00
|
|
|
popup.applyData(hostData.draft || hostData.data, !hostData.draft);
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-14 09:38:59 +02:00
|
|
|
|
2014-05-11 19:19:09 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Protect 'enable checkbox' if was manually modified
|
|
|
|
*/
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.enableCheck.addEventListener('click', function() {
|
|
|
|
this.classList.add('not-auto-change');
|
2015-04-15 21:58:54 +02:00
|
|
|
});
|
2014-06-17 10:44:29 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Save script
|
|
|
|
*/
|
2014-06-17 10:44:29 +02:00
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.saveBtn.addEventListener('click', popup.save);
|
2014-06-17 10:44:29 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Reset script
|
|
|
|
*/
|
2014-06-17 10:44:29 +02:00
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.resetBtn.addEventListener('click', popup.reset);
|
2014-06-25 11:11:33 +02:00
|
|
|
|
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
/**
|
|
|
|
* Remove draft
|
|
|
|
*/
|
|
|
|
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.draftRemoveLink.addEventListener('click', popup.removeDraft);
|
2015-04-15 21:58:54 +02:00
|
|
|
|
2014-06-25 11:11:33 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
}, false);
|