Skip to content

Commit

Permalink
Fix |console| in Nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaier committed Oct 20, 2014
1 parent 7fdca50 commit 7f7c7b1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
68 changes: 67 additions & 1 deletion extension/modules/api/GM_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") + "]";
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion extension/modules/utils/Scriptish_injectScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down

0 comments on commit 7f7c7b1

Please sign in to comment.