1
0
Fork 0

remove api.js

This commit is contained in:
Lukas Winkler 2017-08-27 20:06:02 +02:00
parent 3570ce4f01
commit 1227255503
3 changed files with 44 additions and 66 deletions

View file

@ -1,30 +0,0 @@
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var website = location.protocol + '//' + location.host;
switch (request.method) {
case 'setData':
var syncdata = {};
syncdata[website] = request.customjs;
chrome.storage.sync.set(syncdata);
break;
case 'getData':
chrome.storage.sync.get(website, function(obj) {
var customjs = obj[website] || JSON.parse('false');
sendResponse({customjs: customjs, host: location.host, protocol: location.protocol});
});
break;
case 'removeData':
chrome.storage.sync.remove(website, function() {
});
break;
case 'goTo':
window.location = request.link;
break;
default:
sendResponse({src: '', config: {}});
}
if (request.reload) {
window.location.reload();
}
return true;
});

View file

@ -63,23 +63,23 @@ document.addEventListener('DOMContentLoaded', function() {
apiclb: {
onSelectedTab: function(tabs) {
popup.tabId = tabs[0].id;
chrome.tabs.sendMessage(popup.tabId, {method: "getData", reload: false}, popup.apiclb.onGetData);
var url = new URL(tabs[0].url);
popup.host = url.host;
popup.protocol = url.protocol;
popup.url = popup.protocol + "//" + popup.host;
// chrome.tabs.sendMessage(popup.tabId, {method: "getData", reload: false}, popup.apiclb.onGetData);
chrome.storage.sync.get(popup.url, popup.apiclb.onGetData)
},
onGetData: function(response) {
onGetData: function(items) {
// console.warn(response);
// console.info(chrome.runtime.lastError);
if (!response || typeof response.host !== 'string') {
popup.error();
return;
}
var response = items[popup.url];
/**
* Create 'hosts select'
*/
console.warn(items);
popup.host = response.host;
popup.protocol = response.protocol;
popup.url = popup.protocol + "//" + popup.host;
chrome.storage.sync.get("hosts", function(items) {
var hosts = items.hosts;
@ -98,12 +98,10 @@ document.addEventListener('DOMContentLoaded', function() {
option.setAttribute('selected', 'selected');
}
popup.el.hostSelect.appendChild(option);
if (!response || typeof response.host !== 'string') {
chrome.storage.sync.set({"hosts": hosts});
}
});
// Store host (current included in array) if customjs is defined
if (response.customjs) {
chrome.storage.sync.set({"hosts": hosts});
}
}
);
/**
@ -112,11 +110,11 @@ document.addEventListener('DOMContentLoaded', function() {
// Set-up data pattern if empty
if (!popup.data) {
popup.data = Object.assign(true, {}, popup.emptyDataPattern);
popup.data = Object.assign({}, popup.emptyDataPattern);
}
// Merge host's data to defaults
popup.data = Object.assign(popup.data, response.customjs);
popup.data = Object.assign(popup.data, response);
// ... source is now encoded as base64
if (popup.data.source.indexOf('data:text/javascript;base64,') === 0) {
@ -128,6 +126,8 @@ document.addEventListener('DOMContentLoaded', function() {
popup.data.source = decodeURIComponent(popup.data.source);
}
popup.piwik.loadExpertMode();
// Apply data (draft if exist)
chrome.storage.local.get(popup.url, function(items) {
var text;
@ -175,13 +175,15 @@ document.addEventListener('DOMContentLoaded', function() {
});
popup.el.expertMode.checked = expertMode;
if (!onLoad) {
chrome.storage.sync.set({"expertMode": expertMode});
popup.data.expertMode = expertMode;
var data = {};
data[popup.url] = popup.data;
chrome.storage.sync.set(data);
}
},
loadExpertMode: function() {
chrome.storage.sync.get("expertMode", function(items) {
popup.piwik.setExpertMode(items.expertMode);
});
var expertMode = (typeof popup.data.expertMode === "undefined") ? false : popup.data.expertMode;
popup.piwik.setExpertMode(expertMode);
}
},
generateScriptDataUrl: function(script) {
@ -256,9 +258,6 @@ document.addEventListener('DOMContentLoaded', function() {
// Transform source for correct apply
data.source = popup.generateScriptDataUrl(data.source);
// Send new data to apply
// chrome.tabs.sendMessage(popup.tabId, {method: "setData", customjs: data, reload: true});
var syncdata = {};
syncdata[popup.url] = data;
chrome.storage.sync.set(syncdata);
@ -266,6 +265,8 @@ document.addEventListener('DOMContentLoaded', function() {
// Clear draft
popup.removeDraft();
chrome.tabs.reload(popup.tabId);
// Close popup
window.close();
@ -322,7 +323,7 @@ document.addEventListener('DOMContentLoaded', function() {
*/
popup.el.hostGoToLink.addEventListener('click', function() {
var link = popup.el.hostSelect.value;
chrome.tabs.sendMessage(popup.tabId, {method: "goTo", link: link, reload: false});
chrome.tabs.update(popup.tabId, {url: link});
window.close();
});
@ -457,8 +458,6 @@ document.addEventListener('DOMContentLoaded', function() {
popup.el.draftRemoveLink.addEventListener('click', popup.removeDraft);
popup.piwik.loadExpertMode();
popup.el.expertMode.addEventListener("change", function(event) {
var enabled = event.target.checked;
popup.piwik.setExpertMode(enabled)

View file

@ -8,24 +8,33 @@
"icons": {
"128": "img/icon256.png"
},
"content_scripts": [ {
"js": [ "js/api.js" ],
"matches": [ "<all_urls>" ]
}, {
"all_frames": true,
"js": [ "js/run.js" ],
"matches": [ "<all_urls>" ]
} ],
"content_scripts": [
{
"all_frames": true,
"js": [
"js/run.js"
],
"matches": [
"<all_urls>"
]
}
],
"browser_action": {
"default_icon": "img/icon256.png",
"default_popup": "popup.html"
},
"content_security_policy": "script-src 'self'; object-src 'self'",
"permissions": [ "storage", "http://*/", "https://*/", "tabs" ],
"permissions": [
"storage",
"http://*/",
"https://*/",
"tabs"
],
"default_locale": "en",
"applications": {
"gecko": {
"id": "tempid@lw1.at",
"strict_min_version": "42.0"
}
}}
}
}