크롬 확장 프로그램이 처음이므로 시작하는 데 약간의 문제가 있습니다.크롬 전달 기능에서 전달되는 메시지가 작동하지 않음
우선 내 전반적인 목표는 내 팝업에서 버튼을 클릭하고 DOM 변경 사항이있을 수 있도록하는 것입니다. 내가 올바르게 이해한다면, 이것을하는 방법은 내용 스크립트를 적재하여이 내용 스크립트에 메시지를 보내는 것입니다. 이것은 내가 크롬 개발자 페이지보고에서 가지고있는,하지만 난 콘솔 로그에 아무것도 표시되지 않습니다
manifest.json을
{
"manifest_version": 2,
"name": "Test",
"version": "1.0",
"permissions": [
"tabs", "http://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["content.js"]
}
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
popup.html
<html>
<body>
<script src="popup.js"></script>
</body>
</html>
팝업 된 .js
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
});
content.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 버전이 무엇인가요? 그리고 전반적인 목표에 따르면 'chrome.browserAction.onClicked' (팝업 없음)를 듣고 콘텐츠 스크립트 (https://developer.chrome.com/extensions/content_scripts.html#pi)를 프로그램 방식으로 삽입해야한다고 생각합니다. 이벤트 핸들러에서. –
@ 方 觉 내가 콘텐츠 스크립트에 보내는 메시지는 팝업의 일부 사용자 입력에 따라 다르므로 필요합니다. 내 크롬 버전은 버전 26.0.1410.63입니다. – gsingh2011