From cb50e1cecf14398285e806d65b79569261fbbb87 Mon Sep 17 00:00:00 2001 From: Piro / YUKI Hiroshi Date: Fri, 29 Nov 2013 02:35:58 +0900 Subject: [PATCH] Open tabs from GM_openInTab as children of the tab #614 --- content/treestyletab/windowHelperHacks.js | 94 ++++++++++------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/content/treestyletab/windowHelperHacks.js b/content/treestyletab/windowHelperHacks.js index 5875d275..b0e4ed5a 100644 --- a/content/treestyletab/windowHelperHacks.js +++ b/content/treestyletab/windowHelperHacks.js @@ -315,6 +315,45 @@ TreeStyleTabWindowHelper.overrideExtensionsPreInit = function TSTWH_overrideExte ' gBrowser.treeStyleTab.isVertical) {' )); } + + // Greasemonkey + // https://addons.mozilla.org/firefox/addon/748 + if (TreeStyleTabUtils.getTreePref('compatibility.Greasemonkey')) { + try { + let hitchModule = Components.utils.import('resource://greasemonkey/util/hitch.js', {}); + let hitch = hitchModule.hitch; + if (hitch.toSource().indexOf('TreeStyleTabService') < 0) { + let ns = {}; + Components.utils.import('resource://greasemonkey/third-party/getChromeWinForContentWin.js', ns); + let getChromeWinForContentWin = ns.getChromeWinForContentWin; + hitchModule.hitch = function(aObject, aMethod) { + if (typeof aMethod == 'function' && + aMethod.toSource().indexOf('function openInTab') > -1) { + let originalOpenInTab = aMethod; + /** + * This function must be replaced on scripts in "chrome:" URL, like this. + * Otherwise the original openInTab() will raise violation error. + * Don't move this hack into JS code modules with "resource:" URL. + */ + aMethod = function openInTab(aSafeContentWindow, aURL, aLoadInBackgtound) { + let chrome = getChromeWinForContentWin(aSafeContentWindow); + if (chrome && chrome.TreeStyleTabService) + chrome.TreeStyleTabService.readyToOpenChildTabNow(aSafeContentWindow); + return originalOpenInTab.apply(this, arguments); + }; + } + return hitch.apply(this, arguments); + }; + Components.utils.import('resource://greasemonkey/util.js', ns); + if (ns.GM_util) + ns.GM_util.hitch = hitchModule.hitch; + } + } + catch(e) { + dump('Tree Style Tab: failed to patch to Greasemonkey.\n'); + dump(e+'\n'); + } + } }; TreeStyleTabWindowHelper.overrideExtensionsBeforeBrowserInit = function TSTWH_overrideExtensionsBeforeBrowserInit() { @@ -675,61 +714,6 @@ TreeStyleTabWindowHelper.overrideExtensionsAfterBrowserInit = function TSTWH_ove ); } - // Greasemonkey - // https://addons.mozilla.org/firefox/addon/748 - if (TreeStyleTabUtils.getTreePref('compatibility.Greasemonkey')) { - try { - let hitchModule = Components.utils.import('resource://greasemonkey/util/hitch.js', {}); - let hitch = hitchModule.hitch; - if (hitch.toSource().indexOf('TreeStyleTabService') < 0) { - hitchModule.hitch = function(aObject, aMethod) { - if (typeof aMethod == 'function' && - aMethod.toSource().indexOf('function openInTab') > -1) { - let originalOpenInTab = aMethod; - /** - * This function must be replaced on scripts in "chrome:" URL, like this. - * Otherwise the original openInTab() will raise violation error. - * Don't move this hack into JS code modules with "resource:" URL. - */ - aMethod = function openInTab(aSafeContentWindow, aChromeWindow, aURL, aLoadInBackgtound) { - if (aChromeWindow.TreeStyleTabService) - aChromeWindow.TreeStyleTabService.readyToOpenChildTabNow(aSafeContentWindow); - return originalOpenInTab.apply(this, arguments); - }; - } - return hitch.apply(this, arguments); - }; - } - } - catch(e) { - dump(e+'\n'); - - // hacks for old versions - if ('GM_BrowserUI' in window && 'openInTab' in GM_BrowserUI) { - eval('GM_BrowserUI.openInTab = '+ - GM_BrowserUI.openInTab.toSource().replace( - /(if\s*\(this\.isMyWindow\([^\)]+\)\)\s*\{\s*)(this\.tabBrowser)/, - '$1 TreeStyleTabService.readyToOpenChildTab($2); $2' - ) - ); - } - else if ('@greasemonkey.mozdev.org/greasemonkey-service;1' in Components.classes) { - let service = Components.classes['@greasemonkey.mozdev.org/greasemonkey-service;1'].getService().wrappedJSObject; - if (service && service.__proto__._openInTab) { - let _openInTab = service.__proto__._openInTab; - if (_openInTab.toSource().indexOf('TreeStyleTabService') < 0) { - service.__proto__._openInTab = function() { - let contentWindow = arguments[0]; - let chromeWindow = arguments[1]; - chromeWindow.TreeStyleTabService.readyToOpenChildTabNow(contentWindow); - return _openInTab.apply(this, arguments); - }; - } - } - } - } - } - // SBM Counter // http://miniturbo.org/products/sbmcounter/ if ('SBMCounter' in window &&