代码块单独工作但不能一起工作
Code blocks work separately but not together
我正在为我的 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); ...
您可能需要重新调整一些其他部分,但无论如何很有可能会出现这种情况。
如果这不是问题所在,那么您的代码并不清楚,我有第二个假设,但我会先尝试一下。
此外,这不是您的问题,但您可能有兴趣按键对 objects 的数组进行排序,而不是分离标题、对它们进行排序,然后将它们匹配排序回原位。请参阅 w3schools 中的示例。虽然也许有一个很好的理由来建立你自己的排序。
我正在为我的 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); ...
您可能需要重新调整一些其他部分,但无论如何很有可能会出现这种情况。
如果这不是问题所在,那么您的代码并不清楚,我有第二个假设,但我会先尝试一下。
此外,这不是您的问题,但您可能有兴趣按键对 objects 的数组进行排序,而不是分离标题、对它们进行排序,然后将它们匹配排序回原位。请参阅 w3schools 中的示例。虽然也许有一个很好的理由来建立你自己的排序。