1
0
Fork 0

improved saving

This commit is contained in:
Lukas Winkler 2017-09-01 20:18:27 +02:00
parent e68bd3bc06
commit 8c698d2c03
7 changed files with 90 additions and 77 deletions

View file

@ -60,7 +60,7 @@
"description": "message if a currently not opened webpage is opened"
},
"enable_description": {
"message": "?? für diese Webseite aktivieren",
"message": "aktivieren",
"description": "label for checkbox"
}
}

View file

@ -4,8 +4,32 @@
"description": "name of the extention"
},
"extention_description": {
"message": "Run custom JavaScript on any website.",
"description": "a short description shown in the extention list"
"message": "Try out Piwik, the open source, privacy aware web analytics software, without the need to modify anything.",
"description": "a short description shown in the extention list; should be pretty short, but descriptive"
},
"extention_firefox_description": {
"message": "piwik, analytics, javascript, debugging",
"description": "longer description text for firefox extention page; can use HTML to stucture the content"
},
"extention_chrome_description": {
"message": "piwik, analytics, javascript, debugging",
"description": "longer description text for chrome extention page; can't use any formatting"
},
"extention_tags": {
"message": "piwik, analytics, javascript, debugging, injecting, open source,",
"description": "tags for Firefox addon description (maximum 20)"
},
"extention_screenshot_1": {
"message": "Add the piwik tracking code to every website without access to the website source",
"description": "description for the first screenshot (https://github.com/Findus23/piwik-injector/blob/master/docs/screenshot_firefox_1.png)"
},
"extention_screenshot_2": {
"message": "",
"description": "description for the second screenshot (https://github.com/Findus23/piwik-injector/blob/master/docs/screenshot_firefox_2.png)"
},
"extention_screenshot_3": {
"message": "",
"description": "description for the third screenshot (https://github.com/Findus23/piwik-injector/blob/master/docs/screenshot_firefox_3.png)"
},
"host_label": {
"message": "hosts",
@ -64,7 +88,7 @@
"description": "message if a currently not opened webpage is opened"
},
"enable_description": {
"message": "enable injection for this host",
"message": "enable",
"description": "label for checkbox"
},
"piwik_url_title": {

View file

@ -62,7 +62,7 @@ a:hover {
}
.host__name select {
min-width: 220px;
min-width: 230px;
}
.host__name a {
@ -72,13 +72,14 @@ a:hover {
.host__enable {
text-align: right;
line-height: 34px;
}
.host__enable input {
position: relative;
bottom: -2px;
margin-left: 2px;
input[type=checkbox] {
vertical-align: middle;
}
label span {
vertical-align: middle;
}
/**
@ -94,7 +95,7 @@ a:hover {
.source__editor pre {
margin: 0;
min-height: 200px;
min-height: 220px;
width: 100%;
line-height: 150%;
}
@ -104,6 +105,7 @@ a:hover {
*/
.controls {
margin-top: 1rem;
}
.controls__save,
@ -117,6 +119,6 @@ a:hover {
background: rgb(223, 117, 20);
}
#editor {
#ace-editor {
font-family: monospace
}

View file

@ -114,17 +114,15 @@ document.addEventListener('DOMContentLoaded', function() {
// Merge host's data to defaults
popup.data = Object.assign(popup.data, response);
// ... source is now encoded as base64
if (popup.data.source.indexOf('data:text/javascript;base64,') === 0) {
popup.data.source = popup.data.source.replace('data:text/javascript;base64,', '');
popup.data.source = atob(popup.data.source);
}
else if (popup.data.source.indexOf('data:text/javascript;charset=utf-8,') === 0) {
popup.data.source = popup.data.source.replace('data:text/javascript;charset=utf-8,', '');
popup.data.source = decodeURIComponent(popup.data.source);
popup.piwik.loadExpertMode();
console.warn("HALLO");
if (popup.data.piwik) {
popup.el.piwikURL.value = popup.data.piwik.piwikURL;
popup.el.siteID.value = popup.data.piwik.siteID;
}
popup.piwik.loadExpertMode();
popup.data.originalSource = popup.data.source;
// Apply data (draft if exist)
chrome.storage.local.get(popup.url, function(items) {
@ -151,6 +149,9 @@ document.addEventListener('DOMContentLoaded', function() {
"})();",
handleTrackingCode: function() {
var piwikURL = encodeURI(popup.el.piwikURL.value);
if (!piwikURL.endsWith("/")) {
piwikURL += "/"
}
var siteID = parseInt(popup.el.siteID.value, 10);
if (!siteID || !piwikURL) {
return false;
@ -159,7 +160,7 @@ document.addEventListener('DOMContentLoaded', function() {
js = js.replace("{{PIWIKURL}}", piwikURL);
js = js.replace("{{SITEID}}", String(siteID));
popup.editor.apply(js);
chrome.storage.sync.set({"piwik": {piwikURL: piwikURL, siteID: siteID}});
popup.data.piwik = {piwikURL: piwikURL, siteID: siteID};
},
setExpertMode: function(expertMode, onLoad) {
popup.editor.editorInstance.setOptions({
@ -184,62 +185,37 @@ document.addEventListener('DOMContentLoaded', function() {
popup.piwik.setExpertMode(expertMode);
}
},
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));
}
catch (e) {
b64 += (';charset=utf-8,' + encodeURIComponent(script));
}
return b64;
},
decodeDataUrl: function(dataUrl) {
if (dataUrl.indexOf('data:text/javascript;base64,') === 0) {
dataUrl = dataUrl.replace('data:text/javascript;base64,', '');
return atob(dataUrl);
}
else if (dataUrl.indexOf('data:text/javascript;charset=utf-8,') === 0) {
dataUrl = dataUrl.replace('data:text/javascript;charset=utf-8,', '');
return decodeURIComponent(dataUrl);
} else {
return dataUrl;
}
},
applyData: function(data, notDraft) {
if (data && !notDraft) {
this.el.draftRemoveLink.classList.remove('is-hidden');
}
console.trace(notDraft);
// if (data && !notDraft) {
// this.el.draftRemoveLink.classList.remove('is-hidden');
// }
data = data || this.data;
// Default value for source
data.source = popup.decodeDataUrl(data.source);
if (!data.source) {
data.source = popup.editor.defaultValue;
}
// Set enable checkbox
// popup.data.config.enable = data.config.enable;
console.trace(data.config.enable);
popup.el.enableCheck.checked = data.config.enable;
// Apply source into editor
popup.editor.apply(data.source);
},
getCurrentData: function() {
return {
config: {
enable: popup.el.enableCheck.checked
},
source: popup.editor.editorInstance.getValue()
};
popup.data.config.enable = popup.el.enableCheck.checked;
popup.data.source = popup.editor.editorInstance.getValue();
return popup.data;
},
removeDraft: function() {
chrome.storage.local.remove(popup.url);
popup.applyData();
chrome.storage.sync.get(popup.url, popup.apiclb.onGetData);
popup.el.draftRemoveLink.classList.add('is-hidden');
},
save: function(e) {
@ -251,9 +227,7 @@ document.addEventListener('DOMContentLoaded', function() {
}
var data = popup.getCurrentData();
// Transform source for correct apply
data.source = popup.generateScriptDataUrl(data.source);
console.warn(data);
var syncdata = {};
syncdata[popup.url] = data;
@ -358,18 +332,25 @@ document.addEventListener('DOMContentLoaded', function() {
popup.error();
return false;
}
if ((source || !popup.data.source) && source !== popup.data.source) {
if (source || !popup.data.source) {
var data = {};
data[popup.url] = {draft: draft};
popup.el.draftRemoveLink.classList.remove('is-hidden');
chrome.storage.local.set(data);
if (source !== popup.data.originalSource) {
popup.el.draftRemoveLink.classList.remove('is-hidden');
// Auto switch 'enable checkbox' on source edit
if (!popup.el.enableCheck.classList.contains('not-auto-change')) {
popup.el.enableCheck.checked = true;
// 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');
}
}
console.log("saved");
},
draftAutoSaveInterval = setInterval(draftAutoSave, 1000);
@ -460,12 +441,4 @@ document.addEventListener('DOMContentLoaded', function() {
popup.piwik.setExpertMode(enabled);
});
chrome.storage.sync.get("piwik", function(items) {
if (items && Object.keys(items).length !== 0) {
popup.el.piwikURL.value = items.items.piwikURL;
popup.el.siteID.value = items.piwik.siteID;
}
});
}, false);

View file

@ -5,6 +5,20 @@
document[where || 'head'].appendChild(elm);
}
function generateScriptDataUrl(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));
}
catch (e) {
b64 += (';charset=utf-8,' + encodeURIComponent(script));
}
return b64;
}
var website = location.protocol + '//' + location.host;
chrome.storage.sync.get(website, function(obj) {
var customjs = obj[website];
@ -12,7 +26,7 @@
// Script
if (customjs.source) {
setTimeout(function() {
injectScript(customjs.source, 'body');
injectScript(generateScriptDataUrl(customjs.source), 'body');
}, 250);
}
}

View file

@ -36,7 +36,7 @@
"default_locale": "en",
"applications": {
"gecko": {
"id": "tempid@lw1.at",
"id": "{59f7eef0-6361-4f57-9e7c-7fdbd6a33710}",
"strict_min_version": "42.0"
}
}

View file

@ -21,7 +21,7 @@
<a class="is-hidden" id="goto-host" href="#"></a>
</div>
<div class="pure-u-2-5 host__enable">
<label for="enable">
<label>
<span id="enable-description"></span>
<input id="enable" name="enable" type="checkbox">
</label>
@ -41,7 +41,7 @@
<input type="hidden" name="script" value="">
</div>
<div class="pure-g controls">
<div class="controls">
<button id="save" class="controls__save pure-button pure-button-primary"
name="save"></button>
<button id="reset" class="controls__reset pure-button" name="reset"></button>