1
0
Fork 0

Fix for Cyrillic in source

This commit is contained in:
Dan Hromada 2014-07-26 08:22:51 +02:00
parent d52b9faa89
commit 884d025ca9
3 changed files with 22 additions and 30 deletions

View file

@ -240,12 +240,6 @@
* Set-up data (script, enable, include, extra)
*/
// Backward compatibility (version <1 and 1)
if( response.customjs && response.customjs.src ) {
response.customjs.source = decodeURI(response.customjs.src); // Old format ...
delete response.customjs.src;
}
// Set-up data pattern if empty
if( !popup.data ) {
popup.data = $.extend(true, {}, popup.emptyDataPattern);
@ -259,6 +253,10 @@
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);
}
// Set storage to store data accessible ONLY from current host
popup.storage.setMode(popup.storage.MODE.private);
@ -272,6 +270,19 @@
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));
}
catch(e) {
b64 += (';charset=utf-8,' + encodeURIComponent(script));
}
return b64;
},
applyData: function(data, notDraft) {
if( data && !notDraft ) {
@ -338,7 +349,7 @@
// Transform source for correct apply
data.config.extra = data.config.extra.replace("\n", ';');
data.source = 'data:text/javascript;base64,' + btoa(data.source);
data.source = popup.generateScriptDataUrl(data.source);
// Send new data to apply
chrome.tabs.sendRequest(popup.tabId, {method: "setData", customjs: data, reload: true});

View file

@ -7,21 +7,7 @@
var customjs = localStorage['customjs'];
if( customjs ) {
try {
customjs = JSON.parse(customjs);
}
catch(e) {
// Backward compatibility (version >1)
customjs = {
source: customjs,
config: {
enable: true,
include: '',
extra: ''
}
};
localStorage['customjs'] = JSON.stringify(customjs);
}
customjs = JSON.parse(customjs);
if( customjs.config.enable ) {
@ -39,13 +25,8 @@
});
// Script
if( customjs.source || customjs.src ) {
if( customjs.source ) {
setTimeout(function() {
// Backward compatibility (version 1)
if( customjs.src ) {
customjs.source = 'data:text/javascript,' + customjs.src;
}
injectScript(customjs.source, 'body');
}, 250);
}

View file

@ -4,7 +4,7 @@
"name": "Custom JavaScript for websites ",
"short_name": "customjs",
"description": "Run custom JavaScript on any website.",
"version": "2.1.2",
"version": "2.1.3",
"permissions": [
"storage",
@ -31,4 +31,4 @@
"default_icon": "img/icon.png",
"default_popup": "popup.html"
}
}
}