Skip to content

Instantly share code, notes, and snippets.

@Infocatcher
Created March 6, 2013 12:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Infocatcher/5099151 to your computer and use it in GitHub Desktop.
Save Infocatcher/5099151 to your computer and use it in GitHub Desktop.
Part of https://addons.mozilla.org/firefox/addon/tab-utilities/versions/1.6pre4 – only with "extensions.tabutils.openLinkWithHistory"
var TU_hookCode = TU_hookMethod;
function TU_hookMethod(aStr) {
try {
var namespaces = aStr.split(".");
try {
var object = this;
while (namespaces.length > 1) {
object = object[namespaces.shift()];
}
}
catch (e) {
throw TypeError(aStr + " is not a function");
}
var method = namespaces.pop();
if (typeof object[method] != "function")
throw TypeError(aStr + " is not a function");
return object[method] = TU_hookFunc.apply(this, Array.concat(object[method], Array.slice(arguments, 1)));
}
catch (e) {
Components.utils.reportError("Failed to hook " + aStr + ": " + e.message);
}
}
function TU_hookFunc(aFunc) {
var myCode = aFunc.toString();
for (var i = 1; i < arguments.length;) {
if (arguments[i].constructor.name == "Array") {
var [orgCode, newCode, flags] = arguments[i++];
}
else {
var [orgCode, newCode, flags] = [arguments[i++], arguments[i++], arguments[i++]];
}
if (typeof newCode == "function" && newCode.length == 0)
newCode = newCode.toString().replace(/^.*{|}$/g, "");
switch (orgCode) {
case "{": [orgCode, newCode] = [/{/, "$&\n" + newCode];break;
case "}": [orgCode, newCode] = [/}$/, newCode + "\n$&"];break;
}
if (typeof orgCode == "string")
orgCode = RegExp(orgCode.replace(/[{[(\\^|$.?*+/)\]}]/g, "\\$&"), flags || "");
myCode = myCode.replace(orgCode, newCode);
}
// Cu.reportError(myCode);
// myCode = myCode.replace(/(^.*\n?{)([\s\S]*)(}$)/, function(s, s1, s2, s3) (function() {
// $1
// try {
//// switch (arguments.callee.name) {
//// case "set_selectedTab":
//// Cu.reportError(arguments.callee.caller.name + '*' + arguments.callee.name + '*' + (val && val._tPos));break;
//// case "BrowserOpenTab":
//// Cu.reportError(arguments.callee.caller.name + '*' + arguments.callee.name );break;
//// }
// $2
// } catch (e) {
// Cu.reportError([arguments.callee.name ,e]);
// Cu.reportError(arguments.callee.stack);
// Cu.reportError(arguments.callee);
// }
// $3
// }).toString().replace(/^.*{|}$/g, "").replace("$1", s1).replace("$2", s2).replace("$3", s3));
return eval("(" + myCode + ")");
}
TU_hookCode("gBrowser.loadOneTab",
["{", function() {
var currentTab = this.mCurrentTab;
}],
[/(?=return tab;)/, function() {
if (aReferrerURI/* && TU_getPref("extensions.tabutils.openLinkWithHistory", false)*/) {
let currentHistory = currentTab.linkedBrowser.sessionHistory;
let newHistory = tab.linkedBrowser.sessionHistory.QueryInterface(Ci.nsISHistoryInternal);
for (let i = 0; i <= currentHistory.index; i++) {
newHistory.addEntry(currentHistory.getEntryAtIndex(i, false), true);
}
}
}]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment