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"),
|
|
|
|
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"),
|
2017-08-27 09:32:05 +02:00
|
|
|
error: document.getElementById("error"),
|
2018-04-04 11:45:12 +02:00
|
|
|
matomoForm: document.getElementById("matomo-form"),
|
|
|
|
matomoURL: document.getElementById("matomo-url"),
|
2017-08-27 11:11:00 +02:00
|
|
|
siteID: document.getElementById("site-id"),
|
|
|
|
expertMode: document.getElementById("expert-mode")
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
2017-08-26 19:13:00 +02:00
|
|
|
applyi18n: function() {
|
2017-08-27 17:14:19 +02:00
|
|
|
var translatableIDs = ["error-message", "error-tip", "save", "reset", "goto-host", "enable-description", "host-label", "expert-mode-label", "draft-remove"];
|
2017-08-26 13:54:12 +02:00
|
|
|
translatableIDs.forEach(function(id) {
|
2017-08-27 12:00:37 +02:00
|
|
|
var translateKey = id.replace(/-/g, "_");
|
2017-08-26 13:54:12 +02:00
|
|
|
document.getElementById(id).innerText = chrome.i18n.getMessage(translateKey);
|
|
|
|
});
|
2018-04-04 11:45:12 +02:00
|
|
|
var translatableTitles = ["host", "goto-host", "save", "reset", "draft-remove", "matomo-url", "site-id"];
|
2017-08-27 20:20:57 +02:00
|
|
|
translatableTitles.forEach(function(id) {
|
2017-08-26 19:13:00 +02:00
|
|
|
var translateKey = id.replace("-", "_") + "_title";
|
|
|
|
document.getElementById(id).setAttribute('title', chrome.i18n.getMessage(translateKey));
|
|
|
|
});
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.el.matomoURL.setAttribute("placeholder", chrome.i18n.getMessage("matomo_url_placeholder"));
|
2017-08-27 09:32:05 +02:00
|
|
|
popup.el.siteID.setAttribute("placeholder", chrome.i18n.getMessage("site_id_placeholder"));
|
2017-09-02 21:14:37 +02:00
|
|
|
document.title = chrome.i18n.getMessage("extension_name");
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
host: undefined,
|
2017-08-27 11:11:00 +02:00
|
|
|
url: undefined,
|
2015-04-15 21:58:54 +02:00
|
|
|
emptyDataPattern: {
|
|
|
|
config: {
|
2017-08-27 17:14:19 +02:00
|
|
|
enable: false
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
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;
|
2017-08-27 20:20:57 +02:00
|
|
|
editor.setReadOnly(true);
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
apiclb: {
|
2017-08-26 18:37:56 +02:00
|
|
|
onSelectedTab: function(tabs) {
|
|
|
|
popup.tabId = tabs[0].id;
|
2017-08-27 20:06:02 +02:00
|
|
|
var url = new URL(tabs[0].url);
|
|
|
|
popup.host = url.host;
|
|
|
|
popup.protocol = url.protocol;
|
|
|
|
popup.url = popup.protocol + "//" + popup.host;
|
2017-08-27 20:20:57 +02:00
|
|
|
chrome.storage.sync.get(popup.url, popup.apiclb.onGetData);
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
2017-08-27 20:06:02 +02:00
|
|
|
onGetData: function(items) {
|
|
|
|
var response = items[popup.url];
|
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
|
|
|
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2017-08-27 17:14:19 +02:00
|
|
|
chrome.storage.sync.get("hosts", function(items) {
|
|
|
|
var hosts = items.hosts;
|
|
|
|
if (!hosts) {
|
|
|
|
hosts = [];
|
|
|
|
}
|
|
|
|
// Add current host to list
|
|
|
|
if (hosts.indexOf(popup.url) === -1) {
|
|
|
|
hosts.push(popup.url);
|
|
|
|
}
|
|
|
|
// Fill 'hosts select'
|
|
|
|
hosts.forEach(function(host) {
|
|
|
|
var option = document.createElement('option');
|
|
|
|
option.innerText = host;
|
|
|
|
if (host === popup.url) {
|
|
|
|
option.setAttribute('selected', 'selected');
|
|
|
|
}
|
|
|
|
popup.el.hostSelect.appendChild(option);
|
2017-08-27 20:06:02 +02:00
|
|
|
if (!response || typeof response.host !== 'string') {
|
|
|
|
chrome.storage.sync.set({"hosts": hosts});
|
|
|
|
}
|
2017-08-27 17:14:19 +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) {
|
2017-08-27 20:06:02 +02:00
|
|
|
popup.data = Object.assign({}, 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-27 20:06:02 +02:00
|
|
|
popup.data = Object.assign(popup.data, response);
|
2014-05-07 13:56:35 +02:00
|
|
|
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.matomo.loadExpertMode();
|
|
|
|
if (popup.data.matomo) {
|
|
|
|
popup.el.matomoURL.value = popup.data.matomo.matomoURL;
|
|
|
|
popup.el.siteID.value = popup.data.matomo.siteID;
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
2014-05-12 13:20:19 +02:00
|
|
|
|
2017-09-01 20:18:27 +02:00
|
|
|
popup.data.originalSource = popup.data.source;
|
|
|
|
|
2017-08-27 20:06:02 +02:00
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// Apply data (draft if exist)
|
2017-08-27 17:14:19 +02:00
|
|
|
chrome.storage.local.get(popup.url, function(items) {
|
2017-08-27 18:04:20 +02:00
|
|
|
var text;
|
2017-08-27 17:14:19 +02:00
|
|
|
if (items && Object.keys(items).length !== 0) { // if draft exists
|
2017-08-27 18:04:20 +02:00
|
|
|
text = items[popup.url].draft;
|
2017-08-27 17:14:19 +02:00
|
|
|
}
|
2017-08-27 18:04:20 +02:00
|
|
|
popup.applyData(text);
|
2017-08-27 17:14:19 +02:00
|
|
|
});
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
|
|
|
},
|
2017-08-27 09:32:05 +02:00
|
|
|
|
2018-04-04 11:45:12 +02:00
|
|
|
matomo: {
|
2017-08-27 09:32:05 +02:00
|
|
|
defaultTrackingCode: "var _paq = _paq || [];\n" +
|
|
|
|
"/* tracker methods like \"setCustomDimension\" should be called before \"trackPageView\" */\n" +
|
|
|
|
"_paq.push(['trackPageView']);\n" +
|
|
|
|
"_paq.push(['enableLinkTracking']);\n" +
|
|
|
|
"(function() {\n" +
|
2018-04-04 11:45:12 +02:00
|
|
|
" var u=\"{{MATOMOURL}}\";\n" +
|
2018-04-04 11:48:46 +02:00
|
|
|
" _paq.push(['setTrackerUrl', u+'piwik.php']);\n" +
|
2017-08-27 09:32:05 +02:00
|
|
|
" _paq.push(['setSiteId', '{{SITEID}}']);\n" +
|
|
|
|
" var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n" +
|
2018-04-04 11:45:12 +02:00
|
|
|
" g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);\n" +
|
2017-08-27 09:32:05 +02:00
|
|
|
"})();",
|
|
|
|
handleTrackingCode: function() {
|
2018-04-04 11:45:12 +02:00
|
|
|
var matomoURL = encodeURI(popup.el.matomoURL.value);
|
|
|
|
if (!matomoURL.endsWith("/")) {
|
|
|
|
matomoURL += "/";
|
2017-09-01 20:18:27 +02:00
|
|
|
}
|
2017-08-27 09:32:05 +02:00
|
|
|
var siteID = parseInt(popup.el.siteID.value, 10);
|
2018-04-04 11:45:12 +02:00
|
|
|
if (!siteID || !matomoURL) {
|
2017-08-27 09:32:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-04 11:45:12 +02:00
|
|
|
var js = popup.matomo.defaultTrackingCode;
|
|
|
|
js = js.replace("{{MATOMOURL}}", matomoURL);
|
2017-08-27 09:32:05 +02:00
|
|
|
js = js.replace("{{SITEID}}", String(siteID));
|
2017-08-27 17:14:19 +02:00
|
|
|
popup.editor.apply(js);
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.data.matomo = {matomoURL: matomoURL, siteID: siteID};
|
2017-08-27 11:11:00 +02:00
|
|
|
},
|
|
|
|
setExpertMode: function(expertMode, onLoad) {
|
2017-08-27 12:00:37 +02:00
|
|
|
popup.editor.editorInstance.setOptions({
|
|
|
|
readOnly: !expertMode,
|
|
|
|
highlightActiveLine: expertMode,
|
|
|
|
highlightGutterLine: expertMode
|
|
|
|
});
|
|
|
|
popup.editor.editorInstance.container.style.backgroundColor = expertMode ? "white" : "#eaeded";
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.el.matomoForm.querySelectorAll("input").forEach(function(input) {
|
2017-08-27 11:11:00 +02:00
|
|
|
input.disabled = expertMode;
|
|
|
|
});
|
|
|
|
popup.el.expertMode.checked = expertMode;
|
|
|
|
if (!onLoad) {
|
2017-08-27 20:06:02 +02:00
|
|
|
popup.data.expertMode = expertMode;
|
|
|
|
var data = {};
|
|
|
|
data[popup.url] = popup.data;
|
|
|
|
chrome.storage.sync.set(data);
|
2017-08-27 11:11:00 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
loadExpertMode: function() {
|
2017-08-27 20:06:02 +02:00
|
|
|
var expertMode = (typeof popup.data.expertMode === "undefined") ? false : popup.data.expertMode;
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.matomo.setExpertMode(expertMode);
|
2017-08-27 09:32:05 +02:00
|
|
|
}
|
|
|
|
},
|
2015-04-15 21:58:54 +02:00
|
|
|
applyData: function(data, notDraft) {
|
|
|
|
|
2017-09-01 20:18:27 +02:00
|
|
|
// if (data && !notDraft) {
|
|
|
|
// 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-27 17:14:19 +02:00
|
|
|
|
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-09-01 20:18:27 +02:00
|
|
|
// popup.data.config.enable = data.config.enable;
|
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() {
|
2017-09-01 20:18:27 +02:00
|
|
|
popup.data.config.enable = popup.el.enableCheck.checked;
|
|
|
|
popup.data.source = popup.editor.editorInstance.getValue();
|
|
|
|
return popup.data;
|
2015-04-15 21:58:54 +02:00
|
|
|
},
|
|
|
|
removeDraft: function() {
|
2017-08-27 18:04:20 +02:00
|
|
|
chrome.storage.local.remove(popup.url);
|
2015-04-15 21:58:54 +02:00
|
|
|
popup.applyData();
|
2017-09-01 20:18:27 +02:00
|
|
|
chrome.storage.sync.get(popup.url, popup.apiclb.onGetData);
|
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();
|
|
|
|
|
2017-08-27 17:14:19 +02:00
|
|
|
var syncdata = {};
|
|
|
|
syncdata[popup.url] = data;
|
|
|
|
chrome.storage.sync.set(syncdata);
|
2015-04-15 21:58:54 +02:00
|
|
|
|
|
|
|
// Clear draft
|
|
|
|
popup.removeDraft();
|
|
|
|
|
2017-08-27 20:06:02 +02:00
|
|
|
chrome.tabs.reload(popup.tabId);
|
|
|
|
|
2015-04-15 21:58:54 +02:00
|
|
|
// 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"))) {
|
2017-08-27 17:14:19 +02:00
|
|
|
|
|
|
|
chrome.storage.sync.get("hosts", function(items) {
|
|
|
|
var hosts = items.hosts;
|
|
|
|
var index = hosts.indexOf(popup.url);
|
|
|
|
if (index > -1) {
|
|
|
|
hosts.splice(index, 1);
|
|
|
|
}
|
|
|
|
chrome.storage.sync.set({"hosts": hosts});
|
|
|
|
}
|
|
|
|
);
|
2014-05-12 15:20:54 +02:00
|
|
|
|
2017-08-27 18:19:14 +02:00
|
|
|
chrome.storage.sync.remove(popup.url);
|
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
|
|
|
|
2017-08-26 19:13:00 +02:00
|
|
|
popup.applyi18n();
|
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 22:50:13 +02:00
|
|
|
* Click to goTo host link
|
2015-04-15 21:58:54 +02:00
|
|
|
*/
|
2017-08-25 20:39:10 +02:00
|
|
|
popup.el.hostGoToLink.addEventListener('click', function() {
|
2017-08-25 22:50:13 +02:00
|
|
|
var link = popup.el.hostSelect.value;
|
2017-08-27 20:06:02 +02:00
|
|
|
chrome.tabs.update(popup.tabId, {url: link});
|
2015-04-15 21:58:54 +02:00
|
|
|
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
|
|
|
|
2017-08-26 18:37:56 +02:00
|
|
|
chrome.tabs.query({
|
|
|
|
active: true,
|
|
|
|
lastFocusedWindow: true
|
|
|
|
}, popup.apiclb.onSelectedTab);
|
2014-05-14 09:38:59 +02:00
|
|
|
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.el.matomoForm.querySelectorAll("input").forEach(function(input) {
|
|
|
|
input.addEventListener("change", popup.matomo.handleTrackingCode);
|
2017-08-27 09:32:05 +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-27 09:32:05 +02:00
|
|
|
if (!popup.data) {
|
|
|
|
popup.error();
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-01 20:18:27 +02:00
|
|
|
if (source || !popup.data.source) {
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2017-08-27 17:14:19 +02:00
|
|
|
var data = {};
|
|
|
|
data[popup.url] = {draft: draft};
|
|
|
|
chrome.storage.local.set(data);
|
2017-09-01 20:18:27 +02:00
|
|
|
if (source !== popup.data.originalSource) {
|
|
|
|
popup.el.draftRemoveLink.classList.remove('is-hidden');
|
2014-05-12 16:47:50 +02:00
|
|
|
|
2017-09-01 20:18:27 +02:00
|
|
|
// Auto switch 'enable checkbox' on source edit
|
|
|
|
if (!popup.el.enableCheck.classList.contains('not-auto-change')) {
|
|
|
|
popup.el.enableCheck.checked = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
popup.el.draftRemoveLink.classList.add('is-hidden');
|
2017-08-25 19:44:03 +02:00
|
|
|
}
|
2017-09-01 20:18:27 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
}
|
2017-09-01 20:18:27 +02:00
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
},
|
2017-08-27 17:14:19 +02:00
|
|
|
draftAutoSaveInterval = setInterval(draftAutoSave, 1000);
|
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) {
|
2017-08-27 17:14:19 +02:00
|
|
|
var host = this.value;
|
|
|
|
chrome.storage.sync.get(host, function(items) {
|
|
|
|
var hostData = items[host];
|
|
|
|
if (host !== popup.url) {
|
|
|
|
// Stop making drafts
|
|
|
|
clearInterval(draftAutoSaveInterval);
|
|
|
|
|
|
|
|
// Show goto link
|
|
|
|
popup.el.hostGoToLink.classList.remove('is-hidden');
|
|
|
|
|
|
|
|
// Hide controls
|
|
|
|
popup.el.saveBtn.classList.add('pure-button-disabled');
|
|
|
|
popup.el.resetBtn.classList.add('pure-button-disabled');
|
|
|
|
popup.el.draftRemoveLink.classList.add('is-hidden');
|
|
|
|
|
|
|
|
// Apply other host data
|
|
|
|
try {
|
|
|
|
popup.applyData(hostData, true);
|
|
|
|
}
|
|
|
|
// Hotfix for host without customjs
|
|
|
|
catch (err) {
|
|
|
|
popup.applyData(Object.assign(true, {}, popup.emptyDataPattern), true);
|
|
|
|
}
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
2017-08-27 17:14:19 +02:00
|
|
|
else {
|
|
|
|
// Start making drafts
|
|
|
|
draftAutoSaveInterval = setInterval(draftAutoSave, 1000);
|
|
|
|
|
|
|
|
// Hide goto link
|
|
|
|
popup.el.hostGoToLink.classList.add('is-hidden');
|
|
|
|
|
|
|
|
// Show controls
|
|
|
|
popup.el.saveBtn.classList.remove('pure-button-disabled');
|
|
|
|
popup.el.resetBtn.classList.remove('pure-button-disabled');
|
2017-08-27 18:04:20 +02:00
|
|
|
chrome.storage.local.get(popup.url, function(items) {
|
|
|
|
if (items && Object.keys(items).length !== 0) { // if draft exists
|
|
|
|
popup.applyData(items[popup.url].draft);
|
|
|
|
popup.el.draftRemoveLink.classList.remove('is-hidden');
|
|
|
|
} else {
|
|
|
|
popup.applyData(hostData);
|
|
|
|
popup.el.draftRemoveLink.classList.add('is-hidden');
|
2017-08-27 17:14:19 +02:00
|
|
|
|
2017-08-27 18:04:20 +02:00
|
|
|
}
|
|
|
|
});
|
2015-04-15 21:58:54 +02:00
|
|
|
}
|
2017-08-27 17:14:19 +02:00
|
|
|
});
|
2014-05-11 19:19:09 +02:00
|
|
|
|
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
|
|
|
|
2017-08-27 17:14:19 +02:00
|
|
|
popup.el.expertMode.addEventListener("change", function(event) {
|
2017-08-27 11:11:00 +02:00
|
|
|
var enabled = event.target.checked;
|
2018-04-04 11:45:12 +02:00
|
|
|
popup.matomo.setExpertMode(enabled);
|
2017-08-27 11:11:00 +02:00
|
|
|
});
|
|
|
|
|
2017-08-25 19:44:03 +02:00
|
|
|
}, false);
|