我正在尝试将新标签 URL 从 background.js 发送到 content.js。 .sendMessage() 执行但没有到达 content.js
背景.js:
pd.DataFrame(Q)
def shortest_path(begin,end):
path=[begin]
next_node= np.argmax(Q[begin,])
path.append(next_node)
while next_node != end:
next_node= np.argmax(Q[next_node,])
path.append(next_node)
return path
print (shortest_path(2,10))
content.js:
chrome.tabs.onUpdated.addListener(
function (tabId, changeInfo, tab) {
if (changeInfo.url) {
chrome.tabs.sendMessage(tabId, {
url: changeInfo.url
})
}
}
);
manifest.json:
chrome.runtime.onMessage.addListener(function(request, sender, callback) {
console.log('here');
});
答案 0 :(得分:0)
内容脚本在默认情况下domContentloded之后运行(it can be changed),但是在选项卡开始加载时,将报告URL onupdated。在内容脚本运行之前。
解决方案是添加一个检查以跳过第一次更新,因为它不需要:内容脚本的一个实例在每个匹配的网页中运行,并且它已经知道其页面的 location.href
。
if (changeInfo.url && tab.status === 'complete') {
在 chrome://extensions 页面上重新加载扩展程序(或从网上商店更新它)后,其所有内容脚本都被“孤立”并且无法接收消息。