I have tabs like this:
to move tab A after tab C I use
chrome.tabs.move(tabA.id, {'index': tabC.index+1});
but I get it after tab D
If I move any tab from down to up in the list, it works fine, but if "upper" tab is moved down, indexes get messed (upper tab is removed from indexes list and target index is down by 1)
Do I need to somehow compare moved tab index and target index and based on that calculate resulting index (if A < C) or is there easier solution for this?
I've had some experiences with the Chrome API too, and if there's one thing I learned, then it's that the API just behaves as it does.
If new indices get assigned to tabs before the current one is inserted, but after it's been removed, then that's just that.
It's probably best to just go with a check like you suggested:
function moveAfter(what, target)
{
target = (typeof target === 'number' target : target.index) + 1;
if(what.constructor === Array.prototype.constructor)
{
var w = [];
var t = target;
for(var i = 0; i < what.length)
{
if(what[i].index < target)
{
t--;
}
w.push(what[i].id);
}
what = w;
target = t;
}
else
{
if(what.index < target)
{
target--;
}
what = what.id;
}
chrome.tabs.move(what, {index: target});
}
what
can be a Tab
object or an array of Tab
s.target
can be a Tab
object or a tab index (without the +1
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With