douxian0008 2016-01-09 13:06 采纳率: 0%
浏览 358
已采纳

消息从外部网页传递到chrome插件

I am trying to make a webpage send data to a chrome extension that will fire when user visit a website(say google here)

manifest.json

{
  "manifest_version": 2,
  "name": "Test Addon",
  "version": "0.01",
  "icons": { "16": "icon-16.ico" },

  "browser_action": {
  "default_icon" : "icon-16.ico",
  "default_popup" : "popup.html"
  },

   "content_scripts": [
    {
      "matches": ["*://*.google.com/*"  ],
      "js": ["cs.js"]
    }
  ],

  "externally_connectable": {
  "matches": ["*://*.mywebsite.com/*"]
}


}

cs.js

chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    alert("world");
  });

mywebsite.com/testpage.php

// The ID of my chrome extension (In developer mode)
var editorExtensionId = "cjgeckgdpfhnedenpkaanpehddchlkle";

// Send a message
chrome.runtime.sendMessage("Hello");

</script>

As far as i know content script have access to onMessage and sendMessage but i don't get an alert when i visit google

展开全部

  • 写回答

1条回答 默认 最新

  • douran6443 2016-01-10 00:08
    关注

    According to the docs, the extension id should be the first parameter when calling sendMessage(). You simply omitted this param.
    The second problem is that messages from external web page can receive only the background script. Check the docs for onMessageExternal().
    Use this code to send a message to your background extension script:

    // The ID of my chrome extension (In developer mode)
    var editorExtensionId = "cjgeckgdpfhnedenpkaanpehddchlkle";
    
    // Send a message
    chrome.runtime.sendMessage(editorExtensionId, "Hello");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部