尝试从背景脚本向内容脚本发送消息,但未收到消息。
谷歌搜索了几个小时,但似乎没有任何效果。
内容脚本代码:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("background.js got a message")
console.log(request); // tried with .txt
console.log(sender);
}
);
背景脚本代码:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
if(changeInfo.url != null){
if (changeInfo.url.includes('csfd.cz')) {
console.log("ČSFD detected: " + changeInfo.url);
getNetflixInfo();
}
}
});
function getInfo() {
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://www.randomurl.com");
xhr.setRequestHeader('Content-Type', 'text/html');
xhr.onload = function() {
if (xhr.status === 200) {
var altNameStr = "",
s = xhr.responseText,
re = /data-title="title"><[^<]+>([^<]+)</gmi,
item;
while (item = re.exec(s)) {
//arr.push(item[1]);
altNameStr += '|' + item[1];
}
console.log(altNameStr);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"});
});
}
else {
alert('XHR status = ' + xhr.status)
}
};
xhr.send();
}