Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MIT/X11 License
- *
- * Copyright (c) 2010 Original Author
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * Contributor(s):
- * Erik Vold <[email protected]> (Original Author)
- *
- * ***** END LICENSE BLOCK ***** */
- const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
- Cu.import("resource://gre/modules/Services.jsm");
- const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
- (function(global) global.include = function include(src) (
- Services.scriptloader.loadSubScript(src, global)))(this);
- function main(win) {
- Services.console.logStringMessage("main");
- let doc = win.document;
- let gBrowser = win.gBrowser;
- function $(id) doc.getElementById(id);
- function xul(type) doc.createElementNS(NS_XUL, type);
- var menuitem = xul("menuitem");
- menuitem.setAttribute("label", "Sort Tabs");
- menuitem.addEventListener("command", function() {
- var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
- 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"),
- openFrom = null,
- title = "Sort Tabs",
- 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"),
- input = {value: ""},
- check = {value: false};
- prompts.prompt(openFrom, title, question, input, checkboxText, check);
- var labelOrUrl, stringOrRegexp, compare, innerRegexp = input.value.match(/^\/(.*)\/$/), matchAction;
- if (check.value)
- labelOrUrl = function(a)(a.label); //if we sort by page Titles
- else
- labelOrUrl = function(a)(a.linkedBrowser._contentWindow.location.href); // if we sort by URL
- if (innerRegexp && innerRegexp[1]){ // turn input to regExp if needed
- stringOrRegexp = new RegExp(innerRegexp[1],"ig");
- matchAction = function(str,searchStr)(!!str.match(searchStr));
- }
- else{
- stringOrRegexp = input.value.toLowerCase();
- matchAction = function(str,searchStr)(str.toLowerCase().indexOf(searchStr) != -1);
- }
- if (input.value == "") // if we want just regular sort
- compare = function(a,b)((labelOrUrl(a) > labelOrUrl(b))?1:-1);
- else
- compare = function(a, b) {
- var am = matchAction(labelOrUrl(a),stringOrRegexp);
- var bm = matchAction(labelOrUrl(b),stringOrRegexp);
- return (am&&!bm)?-1:(
- (!am&&bm)?1:
- ((gBrowser.visibleTabs.indexOf(a) > gBrowser.visibleTabs.indexOf(b))?1:-1)
- )
- }
- var tabs = [];
- for (var i = gBrowser.visibleTabs.length - 1; ~i; i--) tabs[i] = gBrowser.visibleTabs[i];
- tabs.sort(compare).forEach(gBrowser.moveTabTo.bind(gBrowser));
- }, true);
- $("tabContextMenu").insertBefore(menuitem, $("context_reloadAllTabs"));
- unload(function() menuitem.parentNode.removeChild(menuitem), win);
- }
- var addon = {
- getResourceURI: function(filePath) ({
- spec: __SCRIPT_URI_SPEC__ + "/../" + filePath
- })
- }
- function disable(id) {
- Cu.import("resource://gre/modules/AddonManager.jsm");
- AddonManager.getAddonByID(id, function(addon) {
- addon.userDisabled = true;
- });
- }
- function install() {}
- function uninstall() {}
- function startup(data) {
- include(addon.getResourceURI("includes/utils.js").spec);
- watchWindows(main, "navigator:browser");
- };
- function shutdown(data, reason) unload()
Advertisement
Add Comment
Please, Sign In to add comment