循环中的更新选项卡在 Chrome 扩展中不起作用
Update tab in Loop Not Working in Chrome Extension Extension
我正在尝试构建一个 chrome 扩展程序,它将在一个选项卡中循环访问一些特定的 URL。我正在使用 chrome 本地存储来存储 URL。这是我尝试更新选项卡的 background.js
部分,
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'sending_links_of_posts') {
changeUrl(request);
}
});
function changeUrl(request) {
const urlArr = request.url;
console.log(urlArr);
chrome.storage.local.set({ urls: urlArr }, function () {
console.log('Urls stored. \n' + urlArr);
});
chrome.storage.local.get(['urls'], function (result) {
for (let i = 1; i < result.urls.length; i++) {
console.log('Value currently is ' + result.urls[i]);
chrome.tabs.update(undefined, {
url: 'https://m.facebook.com' + urlArr[i],
});
}
});
}
Chrome 只打开第一个 link 标签。我在 manifest.json
中获得了 tabs
和 storage
的许可。我试过 chrome.tabs.create
,它会为所有 URL 创建新的标签。但我只需要更新当前选项卡。我正在使用 manifest_version: 2
。
我在这里错过了什么?
不和谐频道的人帮我找到了答案。我要实现的功能可以用这段代码实现。
let i;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'sending_links_of_posts') {
// initialize index
i = 0;
// add urls to local storage
const urlArr = request.url;
console.log(urlArr);
chrome.storage.local.set({ urls: urlArr }, function () {
console.log('Urls stored. \n' + urlArr);
});
// start changing the url
changeUrl(i);
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// only update after the page has loaded
if(changeInfo.status === "complete") {
i++;
changeUrl(i);
}
});
function changeUrl(i) {
chrome.storage.local.get(['urls'], function (result) {
if (i >= result.urls.length) {
return;
}
console.log('Value currently is ' + result.urls[i]);
chrome.tabs.update(undefined, {
url: 'https://m.facebook.com/' + result.urls[i],
});
});
}
将 changeUrl
函数封装到 setTimeout
中或实现 sleep
函数有助于减缓 URL 变化过程。
我正在尝试构建一个 chrome 扩展程序,它将在一个选项卡中循环访问一些特定的 URL。我正在使用 chrome 本地存储来存储 URL。这是我尝试更新选项卡的 background.js
部分,
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'sending_links_of_posts') {
changeUrl(request);
}
});
function changeUrl(request) {
const urlArr = request.url;
console.log(urlArr);
chrome.storage.local.set({ urls: urlArr }, function () {
console.log('Urls stored. \n' + urlArr);
});
chrome.storage.local.get(['urls'], function (result) {
for (let i = 1; i < result.urls.length; i++) {
console.log('Value currently is ' + result.urls[i]);
chrome.tabs.update(undefined, {
url: 'https://m.facebook.com' + urlArr[i],
});
}
});
}
Chrome 只打开第一个 link 标签。我在 manifest.json
中获得了 tabs
和 storage
的许可。我试过 chrome.tabs.create
,它会为所有 URL 创建新的标签。但我只需要更新当前选项卡。我正在使用 manifest_version: 2
。
我在这里错过了什么?
不和谐频道的人帮我找到了答案。我要实现的功能可以用这段代码实现。
let i;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'sending_links_of_posts') {
// initialize index
i = 0;
// add urls to local storage
const urlArr = request.url;
console.log(urlArr);
chrome.storage.local.set({ urls: urlArr }, function () {
console.log('Urls stored. \n' + urlArr);
});
// start changing the url
changeUrl(i);
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// only update after the page has loaded
if(changeInfo.status === "complete") {
i++;
changeUrl(i);
}
});
function changeUrl(i) {
chrome.storage.local.get(['urls'], function (result) {
if (i >= result.urls.length) {
return;
}
console.log('Value currently is ' + result.urls[i]);
chrome.tabs.update(undefined, {
url: 'https://m.facebook.com/' + result.urls[i],
});
});
}
将 changeUrl
函数封装到 setTimeout
中或实现 sleep
函数有助于减缓 URL 变化过程。