代码块可以单独工作,但不能一起工作



我正在为chrome扩展创建一个函数。它应该按照字母顺序将所有选项卡组移到最左边的位置,然后按照字母顺序对其余选项卡进行排序。问题是,当我删除这个代码块时,一切都很好,这意味着tabGroups被移到了最左边的位置。但当我把它加回来时,它就停止工作了。问题是,如果我自己手动移动tabGroups,代码块就可以自己工作。感谢您的帮助!

for (let i = 0; i < titles.length; i++) {
for (let j = 0; j < titles.length; j++) {
if (tabs[i + moveIndex].title == titles[j]) {
chrome.tabs.move(tabs[i + moveIndex].id, { index: (j + moveIndex) });
}
}
}

这是完整的功能,选项卡为:let tabs = await chrome.tabs.query({ currentWindow: true });

async function titleSort(tabs) {
// Puts groups in current window into an array
let groups = await chrome.tabGroups.query({ windowId: -1 });
console.log(groups);
// Separates titles into a different array
let groupTitles = [];
for (let i = 0; i < groups.length; i++) {
groupTitles.push(groups[i].title);
}
// Sorts the array alphabetically
groupTitles.sort((a, b) => a.localeCompare(b));
console.log(groupTitles);
// Put groups into an array in alphabetical order
let groupsAlph = [];
for (let i = 0; i < groups.length; i++) {
for (let j = 0; j < groupTitles.length; j++) {
if (groupTitles[i] == groups[j].title) {
groupsAlph.push(groups[j]);
}
}
}
console.log(groupsAlph);
chrome.storage.sync.get(["preserveGroupOrder"], (data) => {
if (data.preserveGroupOrder == false) {
// Separates titles into a different array
let titles = [];
for (let i = 0; i < tabs.length; i++) {
titles.push(tabs[i].title);
}
// Sorts the array alphabetically
titles.sort((a, b) => a.localeCompare(b));
// Checks if the titles match and rearranges the tabs accordingly
for (let i = 0; i < tabs.length; i++) {
for (let j = 0; j < titles.length; j++) {
if (tabs[i].title == titles[j]) {
chrome.tabs.move(tabs[i].id, { index: j });
}
}
}
} else if (data.preserveGroupOrder == true) {
let tabsInGroup = [];
// Resets values to 0 in tabsInGroup
for (let i = 0; i < groups.length; i++) {
tabsInGroup[i] = 0;
}
// Gets the amount of tabs in each group
for (let i = 0; i < tabs.length; i++) {
for (let j = 0; j < groups.length; j++) {
if (tabs[i].groupId == groupsAlph[j].id) {
tabsInGroup[j]++;
}
}
}
console.log(tabsInGroup);

// Moves groups to the left most positions
let moveIndex = 0;
for (let i = 0; i < groupsAlph.length; i++) {
chrome.tabGroups.move(groupsAlph[i].id, { index: moveIndex });
moveIndex += tabsInGroup[i];
}
console.log(moveIndex);
// Separates titles into a different array
let titles = [];
let tabsLength = tabs.length - moveIndex;
for (let i = 0; i < tabsLength; i++) {
titles.push(tabs[i + moveIndex].title);
}
// Sorts the array alphabetically
titles.sort((a, b) => a.localeCompare(b));
console.log(titles);

// TODO: Sort the rest of the tabs (works separately)
// Checks if the titles match and rearranges the tabs accordingly
for (let i = 0; i < titles.length; i++) {
for (let j = 0; j < titles.length; j++) {
if (tabs[i + moveIndex].title == titles[j]) {
chrome.tabs.move(tabs[i + moveIndex].id, { index: (j + moveIndex) });
}
}
}
}
});
}

我很有信心,问题是在移动选项卡组后没有重新请求选项卡:

chrome.tabGroups.move(groupsAlph[i].id, { index: moveIndex });

如果您只创建一次选项卡索引,那么如果选项卡6-8是一个组的一部分,并且该组被移动到该行的索引点0-2,则您的选项卡变量不知道这一点,除非您重新查询。因此,当你开始重新查询选项卡3到8时,你包括了你不想要的选项卡,而排除了你想要的选项卡。如果出于某种原因你不想重新查询,你可以跟踪你找到选项卡的索引号,让你的标题生成器从零开始,跳过这些选项卡。即,代替->

for (let i = 0; i < tabsLength; i++) {
titles.push(tabs[i + moveIndex].title); ...

类似->

for (let i = 0; i < tabs.length; i++) {
if (isNotGroupMember[i]) {
titles.push(tabs[i].title); ...

你需要重新调整其他部分,但无论如何都有很好的机会。

如果这不是问题所在,从你的代码中还不清楚,我有第二个假设,但我会先尝试一下。

此外,这不是你的问题,但你可能感兴趣的是按键对对象数组进行排序,而不是将标题分离出来,对它们进行排序,然后匹配排序。请参阅w3schools上的示例。尽管也许有一个很好的理由来建立你自己的分类。

最新更新