1
從內容腳本中獲取eventPage的首選方式是什麼?舊方法(chrome.extension.getBackgroundPage
)已被替換爲chrome.runtime.getBackgroundPage
,但chrome.runtime
API不能從內容腳本訪問。如何從內容腳本獲取活動頁面?
從內容腳本中獲取eventPage的首選方式是什麼?舊方法(chrome.extension.getBackgroundPage
)已被替換爲chrome.runtime.getBackgroundPage
,但chrome.runtime
API不能從內容腳本訪問。如何從內容腳本獲取活動頁面?
chrome.extension.getBackgroundPage
方法是從瀏覽器操作頁面訪問後臺頁面。要訪問後臺/事件頁面JS,下面的代碼應該從內容腳本工作:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
在JS,你必須建立一個監聽器監聽來自內容腳本請求的背景頁/活動頁面:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
希望這會有所幫助。內容腳本中的chrome.runtime.getBackgroundPage
將無法使用;你只能發送消息給它(如上所述)。