Guest User

Sort tabs

a guest
Mar 25th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MIT/X11 License
  3. *
  4. * Copyright (c) 2010 Original Author
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * Contributor(s):
  25. * Erik Vold <[email protected]> (Original Author)
  26. *
  27. * ***** END LICENSE BLOCK ***** */
  28.  
  29. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  30. Cu.import("resource://gre/modules/Services.jsm");
  31.  
  32. const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  33.  
  34. (function(global) global.include = function include(src) (
  35. Services.scriptloader.loadSubScript(src, global)))(this);
  36.  
  37. function main(win) {
  38. Services.console.logStringMessage("main");
  39. let doc = win.document;
  40. let gBrowser = win.gBrowser;
  41. function $(id) doc.getElementById(id);
  42. function xul(type) doc.createElementNS(NS_XUL, type);
  43.  
  44. var menuitem = xul("menuitem");
  45. menuitem.setAttribute("label", "Sort Tabs");
  46. menuitem.addEventListener("command", function() {
  47. var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  48.  
  49. var checkboxText = decodeURIComponent("%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BF%D0%BE%20%D0%B8%D0%BC%D0%B5%D0%BD%D0%B8%20%D0%B2%D0%BA%D0%BB%D0%B0%D0%B4%D0%BA%D0%B8"),
  50. openFrom = null,
  51. title = "Sort Tabs",
  52. question = decodeURIComponent("%D0%A7%D1%82%D0%BE%D0%B1%D1%8B%20%D0%BF%D0%B5%D1%80%D0%B5%D0%BC%D0%B5%D1%81%D1%82%D0%B8%D1%82%D1%8C%20%D0%B2%D0%BA%D0%BB%D0%B0%D0%B4%D0%BA%D0%B8%2C%20%D1%81%D0%BE%D0%B4%D0%B5%D1%80%D0%B6%D0%B0%D1%89%D0%B8%D0%B5%20%D1%82%D0%B5%D0%BA%D1%81%D1%82%2C%20%D0%B2%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%BE%2C%0A%D0%B2%D0%B2%D0%B5%D0%B4%D0%B8%D1%82%D0%B5%20%D1%82%D0%B5%D0%BA%D1%81%D1%82%20%D0%B8%D0%BB%D0%B8%20%D1%80%D0%B5%D0%B3%D1%83%D0%BB%D1%8F%D1%80%D0%BD%D0%BE%D0%B5%20%D0%B2%D1%8B%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%B2%20%D0%B2%D0%B8%D0%B4%D0%B5%20%2F...%2F%0A%D0%9E%D1%81%D1%82%D0%B0%D0%B2%D1%8C%D1%82%D0%B5%20%D0%BF%D1%83%D1%81%D1%82%D1%8B%D0%BC%2C%20%D1%87%D1%82%D0%BE%D0%B1%D1%8B%20%D0%BE%D1%82%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C"),
  53. input = {value: ""},
  54. check = {value: false};
  55. prompts.prompt(openFrom, title, question, input, checkboxText, check);
  56. var labelOrUrl, stringOrRegexp, compare, innerRegexp = input.value.match(/^\/(.*)\/$/), matchAction;
  57. if (check.value)
  58. labelOrUrl = function(a)(a.label); //if we sort by page Titles
  59. else
  60. labelOrUrl = function(a)(a.linkedBrowser._contentWindow.location.href); // if we sort by URL
  61. if (innerRegexp && innerRegexp[1]){ // turn input to regExp if needed
  62. stringOrRegexp = new RegExp(innerRegexp[1],"ig");
  63. matchAction = function(str,searchStr)(!!str.match(searchStr));
  64. }
  65. else{
  66. stringOrRegexp = input.value.toLowerCase();
  67. matchAction = function(str,searchStr)(str.toLowerCase().indexOf(searchStr) != -1);
  68. }
  69.  
  70. if (input.value == "") // if we want just regular sort
  71. compare = function(a,b)((labelOrUrl(a) > labelOrUrl(b))?1:-1);
  72. else
  73. compare = function(a, b) {
  74. var am = matchAction(labelOrUrl(a),stringOrRegexp);
  75. var bm = matchAction(labelOrUrl(b),stringOrRegexp);
  76. return (am&&!bm)?-1:(
  77. (!am&&bm)?1:
  78. ((gBrowser.visibleTabs.indexOf(a) > gBrowser.visibleTabs.indexOf(b))?1:-1)
  79. )
  80. }
  81.  
  82. var tabs = [];
  83. for (var i = gBrowser.visibleTabs.length - 1; ~i; i--) tabs[i] = gBrowser.visibleTabs[i];
  84. tabs.sort(compare).forEach(gBrowser.moveTabTo.bind(gBrowser));
  85. }, true);
  86. $("tabContextMenu").insertBefore(menuitem, $("context_reloadAllTabs"));
  87.  
  88. unload(function() menuitem.parentNode.removeChild(menuitem), win);
  89. }
  90.  
  91. var addon = {
  92. getResourceURI: function(filePath) ({
  93. spec: __SCRIPT_URI_SPEC__ + "/../" + filePath
  94. })
  95. }
  96.  
  97. function disable(id) {
  98. Cu.import("resource://gre/modules/AddonManager.jsm");
  99. AddonManager.getAddonByID(id, function(addon) {
  100. addon.userDisabled = true;
  101. });
  102. }
  103.  
  104. function install() {}
  105. function uninstall() {}
  106. function startup(data) {
  107. include(addon.getResourceURI("includes/utils.js").spec);
  108.  
  109. watchWindows(main, "navigator:browser");
  110. };
  111. function shutdown(data, reason) unload()
Advertisement
Add Comment
Please, Sign In to add comment