Skip to content

Commit 8051d78

Browse files
committed
- option whether to apply compat options to required scripts too
- fix script import - fix Event object naming - disallow userscripts to run in strict mode
1 parent 4d60d52 commit 8051d78

File tree

10 files changed

+84
-41
lines changed

10 files changed

+84
-41
lines changed

i18n/de/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@
213213
"GM_compat_options_": {
214214
"message": "GM/FF Kompatibilitäts-Optionen:"
215215
},
216+
"Apply_compatibility_options_to_required_script_too": {
217+
"message": "Anwenden der Kompatibilitätsoptionen auf @require Scripte"
218+
},
216219
"Convert_CDATA_sections_into_a_chrome_compatible_format": {
217220
"message": "Konvertiere CDATA Sektionen in ein Chrome-kompatibles Format"
218221
},

i18n/en/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@
213213
"GM_compat_options_": {
214214
"message": "GM/FF compatibility options:"
215215
},
216+
"Apply_compatibility_options_to_required_script_too": {
217+
"message": "Apply compatibility options to @require scripts too"
218+
},
216219
"Convert_CDATA_sections_into_a_chrome_compatible_format": {
217220
"message": "Convert CDATA sections into a chrome compatible format"
218221
},

src/ask.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ chrome.extension.onMessage.addListener(
204204
function(request, sender, sendResponse) {
205205
if (V) console.log("a: method " + request.method);
206206
if (request.method == "confirm") {
207-
var c = confirm(request.msg);
208-
sendResponse({confirm: c});
207+
var resp = function(c) {
208+
sendResponse({ confirm: c });
209+
};
210+
Helper.confirm(request.msg, resp);
209211
} else if (request.method == "showMsg") {
210212
Helper.alert(request.msg);
211213
sendResponse({});

src/background.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ var convertData = function(convertCB) {
340340
window.setTimeout(cb, _setTimeout);
341341
}
342342
},
343-
{ cond: isNewVersion && versionCmp("2.6", version) == eNEWER,
343+
{ cond: isNewVersion && versionCmp("2.6.83", version) == eNEWER,
344344
fn : function(cb) {
345-
console.log("Update config from " + version + " to 2.6");
345+
console.log("Update config from " + version + " to 2.6.83");
346346
restoreAllScriptsEx(false, cb);
347347
}
348348
},
@@ -2552,8 +2552,10 @@ var runtimeInit = function() {
25522552
var requires = [];
25532553

25542554
script.requires.forEach(function(req) {
2555-
// TODO: option to use compaMo.mkCompat here !?!
25562555
var contents = req.textContent;
2556+
2557+
contents = compaMo.mkCompat(contents, script.options.compat_for_requires ? script : null);
2558+
25572559
requires.push(contents);
25582560
});
25592561

@@ -2574,7 +2576,7 @@ var runtimeInit = function() {
25742576
{ method: "executeScript",
25752577
header: script.header,
25762578
code: oobj.createEnv( script.textContent, script),
2577-
requires: compaMo.mkCompat(requiredSrc, script),
2579+
requires: requiredSrc,
25782580
version: chrome.extension.getVersion(),
25792581
storage: storage,
25802582
script: dblscript,
@@ -3335,15 +3337,19 @@ var validUrl = function(href, cond, n) {
33353337
var t, run = false;
33363338
if (cond.inc || cond.match) {
33373339
for (t in cond.inc) {
3338-
if (matchUrl(href, cond.inc[t])) {
3340+
if (typeof cond.inc[t] !== 'string') {
3341+
console.log("bg: WARN: include[" + t + "] '" + cond.inc[t] + "' " + (n ? "@" + n + " " : "") + "can't be compared to '" + href + "'");
3342+
} else if (matchUrl(href, cond.inc[t])) {
33393343
if (D) console.log("bg: @include '" + cond.inc[t] + "' matched" + (n ? " (" + n + ")" : ""));
33403344
run = true;
33413345
break;
33423346
}
33433347
}
33443348
if (cond.match) {
33453349
for (t in cond.match) {
3346-
if (matchUrl(href, cond.match[t], true)) {
3350+
if (typeof cond.match[t] !== 'string') {
3351+
console.log("bg: WARN: match[" + t + "] '" + cond.match[t] + "' " + (n ? "@" + n + " " : "") + "can't be compared to '" + href + "'");
3352+
} else if (matchUrl(href, cond.match[t], true)) {
33473353
if (D) console.log("bg: @match '" + cond.match[t] + "' matched" + (n ? " (" + n + ")" : ""));
33483354
run = true;
33493355
break;

src/compat.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ var Helper = Registry.get('helper');
1212

1313
var compaMo = {
1414
mkCompat : function(src, script, test) {
15-
if (script.options.compat_metadata || test) src = compaMo.unMetaDataify(src);
16-
if (script.options.compat_foreach || test) src = compaMo.unEachify(src);
17-
if (script.options.compat_arrayleft || test) src = compaMo.unArrayOnLeftSideify(src);
18-
if (script.options.compat_forvarin /* || test */) src = compaMo.fixForVarXStatements(src);
15+
if (script) {
16+
if (script.options.compat_metadata || test) src = compaMo.unMetaDataify(src);
17+
if (script.options.compat_foreach || test) src = compaMo.unEachify(src);
18+
if (script.options.compat_arrayleft || test) src = compaMo.unArrayOnLeftSideify(src);
19+
if (script.options.compat_forvarin /* || test */) src = compaMo.fixForVarXStatements(src);
20+
}
21+
/* it's a shame, but TM does not support "strict mode" scripts at the moment :(
22+
setID + getID are not working due to callee restrictions tmf:322 */
23+
src = src.replace('"use strict"', '"use\u00A0strict"')
24+
1925
return src;
2026
},
2127

src/environment.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ var adjustLogLevel = function() {
2323
EMV |= (logLevel >= 100);
2424
};
2525

26+
// do not rename...
27+
var Event = function() {};
28+
2629
(function() {
2730

2831
var eDOMCONTENTLOADED = "DOMContentLoaded";
@@ -161,7 +164,6 @@ var TM_mEval = function(script, src, requires, addProps) {
161164
var loadListeners = [];
162165
var nodeInserts = { events: [], done: {}, running: null};
163166
var nodeInsertListener = [];
164-
var Event = function() {};
165167

166168
var applyEvent = function(event, props, fn, that) {
167169
var eprops = {
@@ -460,7 +462,7 @@ function TM_addEventListenerFix() {
460462
/* ######### Fixes ############ */
461463

462464
var TM_functionIdFix_getID = function (cnt) {
463-
if (cnt == undefined) cnt = 20;
465+
if (cnt === undefined) cnt = 20;
464466
if (cnt == 0) return null;
465467
if (!this.__tmid && this.caller && this.caller.getID) {
466468
var id = this.caller.getID(cnt-1);

src/fire.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,10 @@ chrome.extension.onMessage.addListener(
10911091
function(request, sender, sendResponse) {
10921092
if (V) console.log("f: method " + request.method);
10931093
if (request.method == "confirm") {
1094-
var c = confirm(request.msg);
1095-
sendResponse({confirm: c});
1094+
var resp = function(c) {
1095+
sendResponse({ confirm: c });
1096+
};
1097+
Helper.confirm(request.msg, resp);
10961098
} else if (request.method == "showMsg") {
10971099
alert(request.msg);
10981100
sendResponse({});

src/helper.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@
110110
};
111111
window.setTimeout(t, 1);
112112
};
113-
113+
114+
var doConfirm = function(msg, cb) {
115+
var t = function() {
116+
var r = confirm(msg);
117+
cb(r);
118+
};
119+
window.setTimeout(t, 1);
120+
};
114121

115122
var toType = function(obj) {
116123
return ({}).toString.apply(obj).match(/\s([a-z|A-Z]+)/)[1];
@@ -139,6 +146,7 @@
139146
isLocalImage: isLocalImage,
140147
getUrlArgs: getUrlArgs,
141148
alert : doAlert,
149+
confirm : doConfirm,
142150
serialize: serialize,
143151
toType : toType,
144152
staticVars : {

src/options.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,15 @@ var createUtilTab = function(tabv) {
394394
}
395395
}
396396

397-
ta.value = JSON.stringify(exp);
397+
return JSON.stringify(exp);
398398
};
399399

400400
var impo = function(src) {
401401
var err = false;
402402
var cnt = 0;
403-
if (ta.value.trim() != "") {
403+
if (src.trim() != "") {
404404
var m = null;
405405
try {
406-
var src = ta.value
407406
m = JSON.parse(src);
408407
} catch (e) {
409408
var t1 = '<body>';
@@ -412,8 +411,8 @@ var createUtilTab = function(tabv) {
412411
var p1 = src.indexOf(t1);
413412
var p2 = src.lastIndexOf(t2);
414413
if (p1 != -1 && p2 != -1) {
415-
ta.value = src.substr(p1 + t1.length, p2 - (p1 + t1.length));
416-
impo();
414+
src = src.substr(p1 + t1.length, p2 - (p1 + t1.length));
415+
impo(src);
417416
}
418417
} else {
419418
Helper.alert(chrome.i18n.getMessage('Unable_to_parse_this_'));
@@ -487,19 +486,20 @@ var createUtilTab = function(tabv) {
487486
Helper.alert('Error: ' + msg);
488487
};
489488

489+
var impo_textarea = function() {
490+
impo(ta.value);
491+
};
492+
490493
var impo_ls = function() {
491494
function onInitFs(fs) {
492-
493495
fs.root.getFile('scripts.tmx', {}, function(fileEntry) {
494-
495496
// Get a File object representing the file,
496497
// then use FileReader to read its contents.
497498
fileEntry.file(function(file) {
498499
var reader = new FileReader();
499500

500501
reader.onloadend = function(e) {
501-
ta.value = this.result;
502-
impo();
502+
impo(this.result);
503503
};
504504

505505
reader.readAsText(file);
@@ -513,9 +513,8 @@ var createUtilTab = function(tabv) {
513513
};
514514

515515
var expo_ls = function() {
516-
expo();
516+
var ta_value = expo();
517517
function onInitFs(fs) {
518-
519518
fs.root.getFile('scripts.tmx', {create: true}, function(fileEntry) {
520519
// Create a FileWriter object for our FileEntry (log.txt).
521520
fileEntry.createWriter(function(fileWriter) {
@@ -529,7 +528,7 @@ var createUtilTab = function(tabv) {
529528

530529
// Create a new Blob and write it to log.txt.
531530
var bb = new BlobBuilder();
532-
bb.append(ta.value);
531+
bb.append(ta_value);
533532
fileWriter.write(bb.getBlob('text/plain'));
534533

535534
}, errorHandler);
@@ -541,32 +540,38 @@ var createUtilTab = function(tabv) {
541540
};
542541

543542
var expo_doc = function() {
544-
expo();
543+
var ta_value = expo();
545544
var bb = new BlobBuilder();
546-
bb.append(ta.value);
545+
bb.append(ta_value);
547546
saveAs(bb.getBlob("text/plain"), "tmScripts.txt");
548547
};
549548

550-
var imp_ta = HtmlUtil.createButton(i.name, i.id + '_i_ta', null, chrome.i18n.getMessage('Import_from_Textarea'), impo);
549+
var expo_textarea = function() {
550+
ta.value = expo();
551+
};
552+
553+
var imp_ta = HtmlUtil.createButton(i.name, i.id + '_i_ta', null, chrome.i18n.getMessage('Import_from_Textarea'), impo_textarea);
551554
var imp_ls = HtmlUtil.createButton(i.name, i.id + '_i_ls', null, chrome.i18n.getMessage('Import_from_SandboxFS'), impo_ls);
552555
var imp_file = cr('input', i.name, i.id + '_i_file', 'file');
553556

554557
var handleFileSelect = function (evt) {
555558
var files = evt.target.files; // FileList object
559+
var data = [];
556560

557-
// Loop through the FileList and render image files as thumbnails.
561+
var run_impo = function() {
562+
impo(data.pop());
563+
};
564+
558565
for (var i = 0, f; f = files[i]; i++) {
559566
var reader = new FileReader();
560-
561567
// Closure to capture the file information.
562568
reader.onload = (function(theFile) {
563569
return function(e) {
564-
ta.value = e.target.result;
565-
impo();
570+
data.push(e.target.result);
571+
window.setTimeout(run_impo, 10);
566572
};
567573
})(f);
568574
reader.readAsText(f);
569-
// reader.readAsDataURL(f);
570575
}
571576
}
572577

@@ -575,7 +580,7 @@ var createUtilTab = function(tabv) {
575580
imp_file.addEventListener('change', handleFileSelect, false);
576581
}
577582

578-
var exp_ta = HtmlUtil.createButton(i.name, i.id + '_e_ta', null, chrome.i18n.getMessage('Export_to_Textarea'), expo);
583+
var exp_ta = HtmlUtil.createButton(i.name, i.id + '_e_ta', null, chrome.i18n.getMessage('Export_to_Textarea'), expo_textarea);
579584
var exp_doc = HtmlUtil.createButton(i.name, i.id + '_e_doc', null, chrome.i18n.getMessage('Export_to_file'), expo_doc);
580585
var exp_ls = HtmlUtil.createButton(i.name, i.id + '_e_ls', null, chrome.i18n.getMessage('Export_to_SandboxFS'), expo_ls);
581586

@@ -1023,6 +1028,9 @@ var createScriptSettingsTab = function(i, tabd) {
10231028
var i_ds = HtmlUtil.createCheckbox(chrome.i18n.getMessage('Enable_sync'),
10241029
{ id: 'do_sync', name: i.name, enabled: i.do_sync },
10251030
co);
1031+
var i_re = HtmlUtil.createCheckbox(chrome.i18n.getMessage('Apply_compatibility_options_to_required_script_too'),
1032+
{ id: 'compat_for_requires', name: i.name, enabled: i.compat_for_requires },
1033+
co);
10261034
var i_md = HtmlUtil.createCheckbox(chrome.i18n.getMessage('Convert_CDATA_sections_into_a_chrome_compatible_format'),
10271035
{ id: 'compat_metadata', name: i.name, enabled: i.compat_metadata },
10281036
co);
@@ -1039,7 +1047,7 @@ var createScriptSettingsTab = function(i, tabd) {
10391047
{ id: 'compat_prototypes', name: i.name, enabled: i.compat_prototypes },
10401048
co);
10411049

1042-
var i_compats = [i_md, i_fe, i_vi, i_al, i_ts ];
1050+
var i_compats = [i_re, i_md, i_fe, i_vi, i_al, i_ts ];
10431051

10441052
var section_opt = crc('div', 'section', i.name, i.id, 'ta_opt');
10451053
var section_opt_head = crc('div', 'section_head', i.name, i.id, 'head_ta_opt');
@@ -2004,8 +2012,10 @@ chrome.extension.onMessage.addListener(
20042012
createOptionsMenu(request.items);
20052013
sendResponse({});
20062014
} else if (request.method == "confirm") {
2007-
var c = confirm(request.msg);
2008-
sendResponse({confirm: c});
2015+
var resp = function(c) {
2016+
sendResponse({ confirm: c });
2017+
};
2018+
Helper.confirm(request.msg, resp);
20092019
} else if (request.method == "showMsg") {
20102020
Helper.alert(request.msg);
20112021
sendResponse({});

src/parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var Script = function() {
4949
this.sync = { fromRemote: false,
5050
seenOnServer: 0 },
5151
this.options = {
52+
compat_for_requires : true,
5253
compat_metadata : false,
5354
compat_foreach : false,
5455
compat_arrayleft : false,

0 commit comments

Comments
 (0)