简体   繁体   English

无法从Chrome扩展程序中的后台脚本向内容脚本发送消息

[英]unable to send message from background script to content script in chrome extension

I cannot send message from my background script to content script except i remove the popup.html from the browser action in the manifest.json. 我无法从后台脚本向内容脚本发送消息,除非我从manifest.json中的浏览器操作中删除了popup.html。 Someone help me 谁来帮帮我

background.js background.js

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tabs) {
  // Send a message to the active tab
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(tabs[0].id, {"message": "clicked_browser_action"});
  });
});

// This block is new! and will be used later
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

content.js content.js

 chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if( request.message == "clicked_browser_action" ) { console.log("Congratulations you can now work!"); // This line is new! chrome.runtime.sendMessage({"message": "open_new_tab", "url": firstHref}); } } ); 

manifest.json manifest.json

  { "name": "Wowprezi lead tool", "version": "1.0", "description": "Extension to find leads and add to sales force!", "author": "Djouonang Landry", "manifest_version": 2, "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'", "background": { "persistent": true, "scripts": ["js/background.js"] }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["js/jquery-3.3.1.min.js","js/content.js"], "all_frames": true } ], "browser_action": { "default_popup": "html/popup.html", "default_title": "Find leads" } } 

popup.html - Except this is removed from the manifest.json i cannot send message from background to content.js. popup.html-除非从manifest.json中删除,否则我无法将消息从后台发送到content.js。 Please note i use the console function to check if the message was sent across to the content.js 请注意,我使用控制台功能来检查消息是否已发送到content.js

According to documentation , the chrome.browserAction.onClicked listener is 根据文档chrome.browserAction.onClicked侦听器是

Fired when a browser action icon is clicked. 单击浏览器操作图标时触发。 Does not fire if the browser action has a popup. 如果浏览器操作弹出,则不会触发。

(bold type added). (已添加粗体)。

Either remove the browserAction popup or move the code you currently have inside the listener to the popup page script. 要么删除browserAction弹出窗口,要么将当前在侦听器中拥有的代码移到弹出页面脚本中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将消息从后台脚本发送到内容脚本,然后在Chrome扩展程序中进行响应 - Send message from background script to content script then response in chrome extension chrome扩展-将消息从内容脚本发送到后台页面,然后再发送回内容脚本? - chrome extension - send message from content script to background page and back to content script? 在 chrome 扩展中,无法从内容脚本向后台脚本发送消息并获得响应 - In a chrome extension, can't send a message from the content script to the background script and get a response 从内容脚本发送消息到后台脚本会破坏chrome扩展 - Sending message from content script to background script breaks chrome extension Chrome扩展程序:将消息从后台发送到注入的脚本 - Chrome Extension: Send Message from Background to Injected Script Chrome扩展程序背景和内容脚本发布消息 - Chrome extension Background and content script posting message Chrome扩展程序-如何将DOM从内容脚本发送到后台页面? - Chrome Extension - How to message DOM from content script to background page? Chrome扩展程序,如何从面板向内容脚本发送消息 - Chrome extension, how to send message from panel to content script 如何将消息从backgroundjs发送到chrome扩展中的内容脚本? - how to send message from backgroundjs to content script in chrome extension? chrome扩展,可将消息从弹出窗口发送到内容脚本 - chrome extension to Send Message from popup to content script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM