백그라운드 페이지에서 내 content_script.js로 메시지를 전달하는 데 문제가 있습니다. 나는 누군가가 내가 틀린 곳을 지적 할 수 있기를 바란다.크롬 tab.create 및 tab.getSelected
background.html
//in a function function myFunction() { chrome.tabs.create({"url":"myurl","selected":true},function(tab){ updateTab(tab.id); }); } //update Created new tab function updateTab(tabId){ chrome.tabs.getSelected(null, function(tab) { makeRequest(tab.id); });} //make request function makeRequest(tabId) { chrome.tabs.sendRequest(tabId, {greeting: "hello"}, function(response) { console.log(response.farewell); }); }
content_script.js
chrome.extension.onRequest.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"}); else sendResponse({}); // snub them. });
manifest.json을
"permissions": [ "tabs","notifications","http://*/*" ], "content_scripts": [ { "matches": ["http://*/*","https://*/*"], "js": ["content_script.js"] }],
내 문제는 R입니다 background.html은 결코 content_script.js에 전달되지 않았기 때문입니다. 새 탭을 만들고 해당 탭을 선택하는 순서에 대해 몇 가지 문제가 있어야한다고 생각합니다. 그러나이 문제를 해결하는 방법을 모르겠습니다. 감사합니다. .
편집 : 내가 지금까지 한 일이 있습니다.
background.html
chrome.browserAction.onClicked.addListener(function(tab) { var tabId = tab.id; chrome.tabs.getSelected(null, function(tab) { validate(tab.url,tabId); }); }); function validate(url,tabId) { var filter = support(url); if(filter!=null) { getHTML(tabId,url,filter); } else{ var notification = webkitNotifications.createHTMLNotification( 'notification.html' // html url - can be relative ); notification.show(); setTimeout(function(){ notification.cancel(); }, 10000); //10 sec } } function getHTML(tabId,url,filter) { console.log("request"); chrome.tabs.sendRequest(tabId, {action:"getHTML"}, function(response) { var html = response.html; console.log(html); var taburl = ("some thing on server"); chrome.tabs.create({"url":taburl,"selected":true}, function(tab){ var tabId = tab.id; chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){ if(changeInfo.status == "loading"){ console.log("loading"); } if(changeInfo.status == "complete"){ chrome.tabs.onUpdated. removeListene(arguments.callee); updateTab(tabId,url,filter,html); } }); }); }); } function updateTab(tabId,url,filter,html) { chrome.tabs.sendRequest(tabId, {action:"updateTab"}, function(response) { //submit form chrome.tabs.executeScript(tabId, {code: 'document.getElementById(\"hiddenbutton\").click();'}); }); }
content_script.js
chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { var action = request.action; console.log(action); if(action == "getHTML") { var html = document.body.innerHTML; console.log(html); sendResponse({html:document.body.innerHTML}); } else{ //do update on page from server sendResponse({}); } });
내가 예상대로 작동하지만, 내가 espically 청취자 을 제거, 이해하지 못하는 몇 가지 포인트가 아직 없습니다 chrome.tabs.onUpdated.removeListene (arguments.callee);. 나는 어떤 사람이 외모를 가질 기회를 가질 수 있으면 좋겠다. 감사.
감사합니다. – user200340