Sort Tabs Button.

General discussion about Custom Buttons, including feature requests.

Sort Tabs Button.

Postby killik » Mon Mar 27, 2017 9:49 pm

Hello.

I found this code from an older topic, which sorts the tabs by domain/title order:
viewtopic.php?f=2&t=94

Code: Select all
/*Code*/
function sortByLabel(a, b) {
  a = a.label.toLowerCase();
  b = b.label.toLowerCase();
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
function sortByDomain(a, b) {
  a = a.linkedBrowser.contentDocument.domain;
  b = b.linkedBrowser.contentDocument.domain;
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
function moveTabToEnd(tab) {
  var index = gBrowser.tabs.length - 1;
  gBrowser.moveTabTo(tab, index);
}
var tabs = new Array();
for (var i = 0; i < gBrowser.tabs.length; i++) {
  tabs[i] = gBrowser.tabs[i];
}
tabs.sort(sortByLabel).sort(sortByDomain).forEach(moveTabToEnd);


The code works but the sorting has some errors.
for example, here the tab with the twitch domain should be moved before the tabs with the youtube domains. but instead, it is moved between the youtube tabs, probably because the title sorting for that specific tab is taking precedence over the domain sorting.
Image

I dont know how to improve the code to avoid such errors, I am a bit novice with Javascript.
If someoen can help me with this, I ll be grateful.
killik
 
Posts: 12
Joined: Fri Aug 03, 2012 6:35 pm

Re: Sort Tabs Button.

Postby morat » Tue Mar 28, 2017 5:21 pm

Post the urls using the copy url button so we can test for problems.

Copy Paste URL
http://custombuttons.sourceforge.net/forum/viewtopic.php?f=4&t=263

e.g.

Before sort:

Code: Select all
https://www.foxitsoftware.com/
https://en.wikipedia.org/
https://developer.mozilla.org/

After sort:

Code: Select all
https://developer.mozilla.org/
https://en.wikipedia.org/
https://www.foxitsoftware.com/

Also,

The code sorts by domain, not base domain.

e.g.

domain = www.foxitsoftware.com
base domain = foxitsoftware.com

Here is how to sort by base domain.

Code: Select all
/*Code*/
function sortByLabel(a, b) {
  a = a.label.toLowerCase();
  b = b.label.toLowerCase();
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
function sortByDomain(a, b) {
  a = a.linkedBrowser.contentDocument.domain;
  b = b.linkedBrowser.contentDocument.domain;
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
function sortByBaseDomain(a, b) {
  var eTLDService = Components.classes["@mozilla.org/network/effective-tld-service;1"].
    getService(Components.interfaces.nsIEffectiveTLDService);
  a = a.linkedBrowser.currentURI;
  b = b.linkedBrowser.currentURI;
  try { a = eTLDService.getBaseDomain(a); } catch (e) { a = a.asciiHost; }
  try { b = eTLDService.getBaseDomain(b); } catch (e) { b = b.asciiHost; }
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
function moveTabToEnd(tab) {
  var index = gBrowser.tabs.length - 1;
  gBrowser.moveTabTo(tab, index);
}
var tabs = new Array();
for (var i = 0; i < gBrowser.tabs.length; i++) {
  tabs[i] = gBrowser.tabs[i];
}
tabs.sort(sortByLabel).sort(sortByBaseDomain).forEach(moveTabToEnd);
morat
 
Posts: 959
Joined: Wed Dec 28, 2011 2:38 pm


Return to General Discussion

cron