我創建了一個基本擴展程序,用於在Google/URL內容滿足特定要求時搜索Google。它在很大程度上起作用,但在擴展的多個實例時失敗。例如,如果我加載選項卡A然後加載選項卡B,但是單擊選項卡A的頁面操作,我將指向搜索選項卡B的內容。多個標籤頁Chrome擴展程序問題
我不知道如何將腳本分散到每個選項卡,以便單擊選項卡A的頁面操作將始終導致搜索選項卡A的東西。如何做到這一點?我會很感激你的建議!
background.js
title = "";
luckySearchURL = "http://www.google.com/search?btnI=I%27m+Feeling+Lucky&ie=UTF-8&oe=UTF-8&q=";
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.title != "") {
title = request.title;
sendResponse({confirm: "WE GOT IT."});
}
});
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if (change.status === "complete" && title !== "") {
chrome.pageAction.show(tabId);
}
});
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.create({url: luckySearchURL + title})
})
contentscript.js
function getSearchContent() {
url = document.URL;
if (url.indexOf("example.com/") > -1)
return "example";
}
if (window === top) {
content = getSearchContent();
if (content !== null) {
chrome.runtime.sendMessage({title: content}, function(response) {
console.log(response.confirm); })
};
}