내 콘텐츠 스크립트에서 내 popup.html에 응답이 표시되지 않습니다. 이 코드가 실행되고 찾기 버튼을 클릭하면 "Hello from response!" 인쇄되지만 변수 응답은 정의되지 않은 것으로 인쇄됩니다. 궁극적 인 목표는 현재 탭의 DOM을 스크립트 파일로 가져와 구문 분석 할 수 있도록하는 것입니다. DOM을 얻기 위해 콘텐츠 스크립트에 단일 메시지를 사용하고 있지만 반환되지 않고 정의되지 않은 것으로 표시됩니다. 가능한 모든 도움을 찾고 있습니다. 감사.크롬 확장 프로그램에서 콘텐츠 스크립트의 정의되지 않은 응답
popup.html :
<!DOCTYPE html>
<html>
<body>
<head>
<script src="script.js"></script>
</head>
<form >
Find: <input id="find" type="text"> </input>
</form>
<button id="find_button"> Find </button>
</body>
</html>
의 manifest.json :
{
"name": "Enhanced Find",
"version": "1.0",
"manifest_version": 2,
"description": "Ctrl+F, but better",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"*://*/*"
],
"background":{
"scripts": ["script.js"],
"persistent": true
},
"content_scripts":[
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js"],
"run_at": "document_end"
}
]
}
script.js :
var bkg = chrome.extension.getBackgroundPage();
function eventHandler(){
var input = document.getElementById("find");
var text = input.value;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
var tab = tabs[0];
var url = tab.url;
chrome.tabs.sendMessage(tab.id, {method: "getDocuments"}, function(response){
bkg.console.log("Hello from response!");
bkg.console.log(response);
});
});
}
content_script.js :
var bkg = chrome.extension.getBackgroundPage();
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if(request.method == "getDOM"){
sendResponse({data : bkg.document});
}else{
sendResponse({});
}
});
몇 가지 발언 : 1. 당신은 메시지를 보내는'{ method : "getDocuments"}', 그러나'{method : "getDOM"}'을 들어라. 2. 메시지가 일치하더라도 배경 페이지의 문서를 다시 보냅니다. 3. 두 개의 인스턴스 인'script.js'를 실행하고 있습니다 : 하나는 백그라운드 페이지에, 다른 하나는 팝업에 있습니다. 4. 배경 페이지, 팝업, 내용 스크립트와 같은 용어를 직접 입력해야합니다. 5. 이벤트 페이지 (비 지속적인 배경 페이지)가 똑같이 효과적이지만 훨씬 "자원 친화적"인 경우 영구적 인 배경 페이지를 사용하고 있습니다. – gkalpak
(그렇습니다. 곧 답변을 드리겠습니다.) – gkalpak
아래에서 제안한 솔루션을 사용해 보셨습니까? 너에게 효과가 있었 니? – gkalpak