Move Tabs, missing permissions?

Hi, I just made up this code:

'use strict';

browser.browserAction.onClicked.addListener(function(aTab, event) {

async function test(){
	
	let allTabs = await browser.tabs.query({});
	//let newOrderTabs = new Array();
	
	allTabs.sort(compareEqual);
	console.log("allTabs should now be sorted!");
	let allTabIds = allTabs.map((tabInfo) => tabInfo.id);
	console.log("gooten ids array!");
	let moving = browser.tabs.move(allTabIds, {index: 0});
	console.log("all tabs should now have been moved!");
	
}


//run that code!

test();

//2 tabs.Tab objects are equal if their base url part and their title are the same
async function compareEqual(taba,tabb){
	let urla=(new URL(taba.url)).hostname;
	let urlb=(new URL(tabb.url)).hostname;
			
	let titlea=taba.title;   
	let titleb=tabb.title;   
	
	let titleequal=titlea.localeCompare(titleb, 'en', { sensitivity: 'base' });
	let urlequal=urla.localeCompare(urlb, 'en', { sensitivity: 'base' });
	
	//<-1,=0,>1
	
	if(urlequal!=0){return urlequal;}
	else if(titleequal!=0){return titleequal;}
	return 0;
	
}

});

what i intend it to do:
sort all tabs first by hostname, then by title.
and move tabs accordingly to their respective position.

I jsut tried running this, but It didnt do anything.
so I would assume that I am most likely lacking permissions.

Currently I only have tabs permission lsited in my manifest file, what other permissions woudl I need to do what I want here?

Hi @bernd7

I moved your topic to the “Add-ons Development” category, because there are the experts for permissions.

Good luck :slightly_smiling_face:

1 Like

You can’t use async compare function:
async function compareEqual

Remove “async” and it should work. (async function always returns a promise)
Having tabs permission should be enough.

As a side note, you really should use camelCase in your variable names, thisiscrazyunreadable as you can see :slight_smile:.

1 Like

Thanks it totally worked without the async in it :slight_smile:

I dont really know what this while asnyc tuff is all about, I just thought makign everything async shoul do the trick since firefox/javascript expects async everywhere.

No idea why we cant use normal functions in the first place, I’m jsut happy that it works now :slight_smile:

A lot of API in the JavaScript (especially those for addons) are asynchronous - they return a promise. And handling promises is not nice looking, you need to use .then() callback which then is called when the promise is resolved. By using async function, you can use await instead and the function will wait on that line for the promise to be resolved before continuing.

So it’s a very handy “syntactic sugar” that everyone uses these days.

Rule of thumb: if your function doesn’t have a single “await” inside, it probably shouldn’t be async.
See also docs:

1 Like

yeah I kind of know how to use them, with lines like
let a=await somePromisetuff();

but I dont get ehy they even are a thing and not jsut normal methods and such.
like, in the end, it returns a promise that in a nutshell delivers the kind of value (int , string, bool, etc.) that the method would return without being async.

sure, has something to do with the computer waiting for it to be finished before continuing.

makes me wonder why in languages like java, there is a simple thread.sleep(500) for “sleeping” like .5 seconds but javscript is like a gone mad broken train running t 100 mps that can only be stopped by throwing extrea made up promises and stuff in it’s way.

like, one wants code to be executed one line after another,
why doesnt javascript have these like any other language has too?

I dont get the point of the whole promising stuff.

sounds really like “sugar” to me, nice looking new concept but no idea why the language couldnt be made to work without it.

Things are fundamentally different in other languages :slight_smile:, javascript has only a “single thread”, so if you would make it sleep for a minute on some line, nothing else would be able to run and the whole page would froze.

Having only a single thread makes things much simpler :slight_smile: because you don’t have to worry about other threads messing up your state.

In any case, if you mark function as “async” it automatically returns a promise. So if you return “hello world”, it actually returns a Promise<string> which will resolve to “hello world”. And many functions, including .sort expects a number, not a promise.

This is one of the reasons people prefer TypeScript these days, because it would caught that bug.

1 Like

I only know a bit about javascript so far, let alone learn a completely new language now…