我目前正在编写一个扩展,它注入了一个内容脚本。此脚本解析页面中的数据,将其保存在localStorage中,并能够找到下一页的链接并通过location.href = "newUrl"重新加载。在这个页面上,脚本应该再次运行,解析数据,保存数据,转到下一页等等。
目前,我无法找到让我完成最后一部分的解决方案(转到新页面,再次运行脚本,转到下一页)。我找不到像“在加载页面时执行解析函数,然后执行goToNextPage函数”这样的方法。
任何线索都会很感激的!
(从评论中添加)
我的background.js包括以下内容:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(tab.id, {file: "jquery-2.1.3.min.js"}, function () {
chrome.tabs.executeScript(tab.id, {file: "content.js"});
});
});content.js还包括一些读取“下一步”链接、解析当前页面等功能。
发布于 2015-04-08 21:45:43
对于你如何切换页面,我会采取不同的做法。
我会用下一个URL传递背景信息,让后台更新页面并重新注入内容。这样,它将与导航同步。
// Content script
chrome.runtime.sendMessage({action: "updateMe", url: nextUrl});
// Background
function injectScripts(tab) {
chrome.tabs.executeScript(tab.id, {file: "jquery-2.1.3.min.js"},
function () { chrome.tabs.executeScript(tab.id, {file: "content.js"}); }
);
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message.action == "updateMe") {
chrome.tabs.update(sender.tab.id, {url: message.url}, injectScripts);
}
});
// You can use the same handler in onClicked
chrome.browserAction.onClicked.addListener(injectScripts);https://stackoverflow.com/questions/29524068
复制相似问题