1. <?xml version="1.0"?>
  2.  
  3. <overlay id="tabsopenrelative" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  4.  
  5. <!--
  6. Copyright (c) 2006 John Mellor
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. -->
  26.  
  27. <script><![CDATA[ window.addEventListener("load", function(event) {
  28.  
  29. // Globals
  30. window.tabsopenrelative_nextTabShift = 1;
  31. window.tabsopenrelative_nextIsRelative = null;
  32.  
  33. // Reset shift upon switching tabs
  34. getBrowser().mTabContainer.addEventListener("select", function() { tabsopenrelative_nextTabShift = 1; }, false);
  35.  
  36. // Decrement shift if tab closed in shift region (recent children of current tab)
  37. gBrowser.mTabContainer.addEventListener("DOMNodeRemoved", function(event) {
  38.     try {
  39.         var index = event.target._tPos;
  40.         if (index && gBrowser.mTabContainer.selectedIndex < index && gBrowser.mTabContainer.selectedIndex + tabsopenrelative_nextTabShift > index) {
  41.             tabsopenrelative_nextTabShift--;
  42.         }
  43.     }
  44.     catch (ex) {}
  45. }, false);
  46.  
  47. // Mark certain tab sources as always related to current tab
  48. var relatedTabSources = [
  49.     'nsContextMenu.prototype.openLinkInTab',
  50.     'nsContextMenu.prototype.openFrameInTab', // TODO: return statement makes our second replacement be unreachable code, causing a warning, though it's actually fine as that line is unnecessary since this always opens a tab
  51.     'nsContextMenu.prototype.viewBGImage',
  52.     'nsContextMenu.prototype.addDictionaries'
  53.     // TODO: And <menuitem id="menu_HelpPopup_reportPhishingtoolmenu">
  54.     // As well as these, functions recognised in our addTab replacement will count as related
  55. ];
  56. if ('viewMedia' in nsContextMenu.prototype) // [Fx3.5+]
  57.     relatedTabSources.push('nsContextMenu.prototype.viewMedia');
  58. else // [Fx3-]
  59.     relatedTabSources.push('nsContextMenu.prototype.viewImage');
  60. relatedTabSources.forEach(function (fname) {
  61.     eval(fname + "=" + eval(fname + ".toString()").replace(
  62.         '{', // First '{'
  63.         '{ tabsopenrelative_nextIsRelative = true;'
  64.     ).replace(
  65.         /\}$/, // Last '}'
  66.         'tabsopenrelative_nextIsRelative = null; }'
  67.     ));
  68. });
  69.  
  70. // Mark Javascript popups are related to current tab, but links from external applications as unrelated
  71. var orig = eval("nsBrowserAccess.prototype.openURI = " + nsBrowserAccess.prototype.openURI.toString().replace(
  72.    'var newTab',
  73.  
  74.    'tabsopenrelative_nextIsRelative = !isExternal; \
  75.    $&'
  76. ).replace(
  77.     'let win, needToFocusWin;', // For some reason, evaling this function containing a let (in Fx 3.5 at least) causes errors
  78.  
  79.     'var win, needToFocusWin;'
  80. ));
  81.  
  82. nsBrowserAccess.prototype.openURI = function(aURI, aOpener, aWhere, aContext) {
  83.   if (aWhere == Components.interfaces.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW
  84.     && aContext == Components.interfaces.nsIBrowserDOMWindow.OPEN_EXTERNAL
  85.     && gPrefService.getPrefType("browser.link.open_external") == gPrefService.PREF_INT) {
  86.     arguments[2] = gPrefService.getIntPref("browser.link.open_external");
  87.   }
  88.   return orig.apply(this, arguments);
  89. }
  90.  
  91. // Make final decision as to whether new tab is related to current tab or not,
  92. // using call stack if necessary, then move it if appropriate
  93. eval("gBrowser.addTab = " + gBrowser.addTab.toString().replace(
  94.    'if (t.previousSibling.selected)',
  95.  
  96.    'if (tabsopenrelative_nextIsRelative === null) { \
  97.        if (getBoolPref("extensions.tabsopenrelative.linksonly", true)) { \
  98.            /* Only open relative if addTab was called from a few known functions, \
  99.             * such as handleLinkClick which handles Ctrl/middle+clicked links, \
  100.             * that are definitely related to the current tab */ \
  101.            const lvl1RelativeSources = ["webdeveloper_generateDocument"]; \
  102.            const lvl2RelativeSources = ["BrowserSearch_search"]; \
  103.            const lvl3RelativeSources = ["handleLinkClick", "BrowserReloadOrDuplicate", "BrowserForward", "BrowserBack", "gotoHistoryIndex"]; \
  104.            const lvl4RelativeSources = ["diggerLoadURL"]; \
  105.            const lvl5RelativeSources = ["goup_up"]; \
  106.            if (arguments.callee.caller \
  107.                && (lvl1RelativeSources.indexOf(arguments.callee.caller.name) != -1 \
  108.                    || arguments.callee.caller.caller \
  109.                       && (lvl2RelativeSources.indexOf(arguments.callee.caller.caller.name) != -1 \
  110.                           || arguments.callee.caller.caller.caller \
  111.                              && (lvl3RelativeSources.indexOf(arguments.callee.caller.caller.caller.name) != -1 \
  112.                                  || arguments.callee.caller.caller.caller.caller \
  113.                                     && (lvl4RelativeSources.indexOf(arguments.callee.caller.caller.caller.caller.name) != -1 \
  114.                                         || arguments.callee.caller.caller.caller.caller.caller \
  115.                                            && lvl5RelativeSources.indexOf(arguments.callee.caller.caller.caller.caller.caller.name) != -1))))) \
  116.            { \
  117.                tabsopenrelative_nextIsRelative = true; \
  118.            } \
  119.            else { \
  120.                tabsopenrelative_nextIsRelative = false; \
  121.            } \
  122.        } \
  123.        else { \
  124.            /* Always open relative unless addTab was called from a few known functions, \
  125.             * such as sss_undoCloseTab, which move the tab themselves */ \
  126.            const lvl1NonRelativeSources = ["sss_undoCloseTab", "sss_restoreWindow"]; \
  127.            if (arguments.callee.caller \
  128.                && lvl1NonRelativeSources.indexOf(arguments.callee.caller.name) != -1) \
  129.            { \
  130.                tabsopenrelative_nextIsRelative = false; \
  131.            } \
  132.            else { \
  133.                tabsopenrelative_nextIsRelative = true; \
  134.            } \
  135.        } \
  136.    } \
  137.    /* else tabsopenrelative_nextIsRelative was set by one of relatedTabSources or nsBrowserAccess.prototype.openURI */ \
  138.    if (tabsopenrelative_nextIsRelative) { \
  139.        this.moveTabTo(t, this.mCurrentTab._tPos + tabsopenrelative_nextTabShift); \
  140.        tabsopenrelative_nextTabShift++; \
  141.    } \
  142.    tabsopenrelative_nextIsRelative = null; \
  143.    $&'
  144. ));
  145.  
  146. }, false);]]></script>
  147.  
  148. </overlay>
  149.