From 7f7c7b1650d80537140774e16773b67d4e5e297f Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Tue, 21 Oct 2014 01:15:03 +0200 Subject: [PATCH] Fix |console| in Nightly --- extension/modules/api/GM_console.js | 68 ++++++++++++++++++- .../modules/utils/Scriptish_injectScripts.js | 2 +- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/extension/modules/api/GM_console.js b/extension/modules/api/GM_console.js index 67b64d84..044aa645 100644 --- a/extension/modules/api/GM_console.js +++ b/extension/modules/api/GM_console.js @@ -32,7 +32,7 @@ function getConsoleFor(contentWindow, chromeWindow) { }; } -function GM_console(script, contentWindow, chromeWindow) { +function GM_console_legacy(script, contentWindow, chromeWindow) { const _console = getConsoleFor(contentWindow, chromeWindow); const console = { __exposedProps__: {__noSuchMethod__: "r"} }; const prefix = "[" + (script.id || "Scriptish") + "]"; @@ -86,3 +86,69 @@ function GM_console(script, contentWindow, chromeWindow) { return console; } + +function GM_console(sandbox, script, contentWindow, chromeWindow) { + if (!("createObjectIn" in Cu) || !("exportFunction" in Cu)) { + return GM_console_legacy(script, contentWindow, chromeWindow); + } + + const _console = getConsoleFor(contentWindow, chromeWindow); + const console = Cu.createObjectIn(sandbox, {defineAs: "console"}); + const prefix = "[" + (script.id || "Scriptish") + "]"; + + // Wrap log functions + // Redirect any missing log function to .log + for (let i = 0, e = log_functions.length; i < e; ++i) { + let fn = log_functions[i]; + if (fn in _console) { + Cu.exportFunction(_console[fn].bind(_console, prefix), console, { + defineAs: fn + }); + } + else if (fn == "trace") { + let trace = function() { + let args = Array.slice(arguments); + let msg = ""; + + // Skip the top two frames + let stack = Components.stack.caller; + if (stack && (stack = stack.caller)) { + for (let i = 0; i < 10 && stack; ++i, stack = stack.caller) { + msg += "\n[@" + stack.filename + ":" + stack.lineNumber + "]"; + } + args.push(msg); + } + console.log.apply(console, args); + }; + Cu.exportFunction(trace, console, {defineAs: "trace"}); + } + else { + Cu.exportFunction(_console.log.bind(_console, prefix), console, { + defineAs: fn + }); + } + } + + // Wrap aux functions + for (let i = 0, e = aux_functions.length; i < e; ++i) { + let fn = aux_functions[i]; + if (fn in _console) { + Cu.exportFunction(_console[fn].bind(_console, prefix), console, { + defineAs: fn + }); + } + } + + const nsm = function(id, args) { + if (aux_functions.indexOf(id) != -1) { + let fn = _console[id] || (function() {}); + return fn.apply(_console, args); + } + console.log("No such method in console", id); + }; + Cu.exportFunction(nsm, console, { + defineAs: "__noSuchMethod__" + }); + + return console; +} diff --git a/extension/modules/utils/Scriptish_injectScripts.js b/extension/modules/utils/Scriptish_injectScripts.js index 7aa5fc57..4ac8501e 100644 --- a/extension/modules/utils/Scriptish_injectScripts.js +++ b/extension/modules/utils/Scriptish_injectScripts.js @@ -88,7 +88,7 @@ function Scriptish_injectScripts(options) { } lazy(sandbox, "console", function() { - return GM_console(script, safeWin, chromeWin); + return GM_console(sandbox, script, safeWin, chromeWin); }); if (!useGrants || script.grant['GM_log']) {