【问题标题】:Call chrome extension function and get response调用 chrome 扩展功能并获得响应
【发布时间】:2016-10-30 08:04:51
【问题描述】:

我有一个网站“http://localhost/sample”。我需要在扩展中调用一个函数来回显一个单词并获得响应。如何实现这样的代码,需要什么权限

【问题讨论】:

    标签: google-chrome google-chrome-extension extension-methods google-chrome-app


    【解决方案1】:

    使用AjaxWebsocketNativemessaging(您需要nativeMessaging 权限)或任何其他通信方式在扩展程序和本地服务器之间交换数据

    以下代码发送一个简单的http get请求并输出响应。

    您可能还想使用Fetch API 代替 Ajax,这取决于您的需要。

    manifest.json

    {
        ...
        "permissions": [
            "http://localhost/sample"
        ]
        ...
    }
    

    background.js

    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        console.log(xhr.responsetText);
    };
    xhr.open('GET', 'http://localhost/sample');
    xhr.send();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2012-11-26
      • 2012-11-17
      • 2015-05-07
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多