URL/HTML 콘텐츠가 특정 요구 사항을 충족하는지 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); })
};
}