1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00
CanvasBlocker/lib/callingStack.js

103 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2016-08-06 19:17:36 +02:00
/* global exports */
2015-09-08 11:42:32 +02:00
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const preferences = require("sdk/simple-prefs");
const prefs = preferences.prefs;
// Translation
var translate = require("sdk/l10n").get;
2016-02-13 12:28:36 +01:00
var _ = function(name, replace, translateAPI){
2015-09-08 11:42:32 +02:00
"use strict";
2016-02-13 12:28:36 +01:00
if (!translateAPI){
translateAPI = translate;
}
2015-09-08 11:42:32 +02:00
2016-02-13 12:28:36 +01:00
var str = translateAPI(name) || name;
if (replace){
// replace generic content in the transation by given parameter
Object.keys(replace).forEach(function(name){
str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]);
});
}
return str;
};
// Stack parsing
function parseStackEntry(entry){
2015-09-08 11:42:32 +02:00
"use strict";
var m = /@(.*):(\d*):(\d*)$/.exec(entry) || ["", entry, "--", "--"];
return {
url: m[1],
2015-12-31 13:37:27 +01:00
line: parseInt(m[2], 10),
column: parseInt(m[3], 10),
raw: entry
};
}
2015-12-31 13:37:27 +01:00
function stackRuleMatch(stackEntry, stackRule){
if (!stackEntry){
return false;
}
if (stackEntry.url !== stackRule.url){
return false;
}
if ((typeof stackRule.line) !== "undefined" && stackEntry.line !== stackRule.line){
return false;
}
if ((typeof stackRule.column) !== "undefined" && stackEntry.column !== stackRule.column){
return false;
}
return true;
}
// parse calling stack
2016-02-13 12:28:36 +01:00
function parseErrorStack(errorStack){
2015-09-08 11:42:32 +02:00
"use strict";
2016-02-13 12:28:36 +01:00
var callers = errorStack.trim().split("\n");
var findme = callers.shift(); // Remove us from the stack
findme = findme.replace(/(:[0-9]+){1,2}$/, ""); // rm line & column
// Eliminate squashed stack. stack may contain 2+ stacks, but why...
var inDoubleStack = false;
callers = callers.filter(function(caller){
var doubleStackStart = caller.search(findme) !== -1;
inDoubleStack = inDoubleStack || doubleStackStart;
return !inDoubleStack;
2015-12-31 13:37:27 +01:00
}).map(parseStackEntry);
return {
2016-02-13 12:28:36 +01:00
toString: function(translateAPI){
2015-12-31 13:37:27 +01:00
var msg = "";
2016-02-13 12:28:36 +01:00
msg += "\n\n" + _("sourceOutput", undefined, translateAPI) + ": ";
2015-12-31 13:37:27 +01:00
if (prefs.showCompleteCallingStack){
msg += callers.reduce(function(stack, c){
2016-02-13 12:28:36 +01:00
return stack + "\n\t" + _("stackEntryOutput", c, translateAPI);
2015-12-31 13:37:27 +01:00
}, "");
}
else{
2016-02-13 12:28:36 +01:00
msg += _("stackEntryOutput", callers[0], translateAPI);
2015-12-31 13:37:27 +01:00
}
return msg;
},
match: function(stackRule){
if (typeof stackRule.stackPosition !== "undefined"){
var pos = stackRule.stackPosition;
if (pos < 0){
pos += callers.length;
}
return stackRuleMatch(callers[pos], stackRule);
}
else {
return callers.some(function(stackEntry){
return stackRuleMatch(stackEntry, stackRule);
});
}
}
};
}
2016-02-13 12:28:36 +01:00
exports.parseErrorStack = parseErrorStack;