將消息從后臺腳本發(fā)送到內容腳本,然后發(fā)送到注入的腳本我正在嘗試將消息從后臺頁面發(fā)送到內容腳本,然后將該內容腳本中的消息發(fā)送到注入的腳本。我試過這個,但它沒有用。這是我的代碼的樣子。的manifest.json{
"manifest_version": 2,
"name": "NAME",
"description": ":D",
"version": "0.0",
"permissions": [
"tabs","<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}
],
"web_accessible_resources": [
"injected.js"
],
"background":{
"scripts":["background.js"]
}}background.jschrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response){});});content_script.jsvar s = document.createElement('script');s.src = chrome.extension.getURL('injected.js');s.onload = function(){
this.parentNode.removeChild(this);};(document.head||document.documentElement).appendChild(s);chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
document.dispatchEvent(new CustomEvent('Buffer2Remote', {todo: "LOL"}));});injected.jsdocument.addEventListener('Buffer2Remote', function(e){
alert(e.todo);});消息發(fā)送在第一部分background - > content_script中不起作用。我的代碼有什么問題嗎?
將消息從后臺腳本發(fā)送到內容腳本,然后發(fā)送到注入的腳本
動漫人物
2019-09-02 05:04:29