【问题标题】:Constantly sending data with ajax in chrome extension在 Chrome 扩展中不断使用 ajax 发送数据
【发布时间】:2013-03-24 17:38:34
【问题描述】:

是否可以将数据从 google chrome 扩展程序发布到另一个页面,例如每分钟?有人打开 chrome,然后每分钟都会有信息发送到我的页面。谢谢您的回答。

【问题讨论】:

    标签: ajax google-chrome google-chrome-extension


    【解决方案1】:

    是的,这很有可能。一个简单的例子:

    背景页面

    //These make sure that our function is run every time the browser is opened.
    chrome.runtime.onInstalled.addListener(function() {
      initialize();
    });
    chrome.runtime.onStartup.addListener(function() {
      initialize();
    });
    function initialize(){
      setInterval(function(){sendData()},60000);
    }
    function sendData(){
      //Assuming data contains the data you want to post and url is the url
      $.post(url,data);
    }
    

    Manifest.json

    我们需要为我们发布的位置请求主机权限。类似于“http://www.example.com/postHere.php”的东西。请参阅Match Patterns 了解更多信息。

    {
      "name": "Chrome post test",
      "version": "0.1",
      "description": "A test for posting",
      "manifest_version": 2,
      "permissions": [
        "http://www.example.com/postHere.php"
      ],
      "background": {
        "scripts": ["jquery-1.8.3.min.js","background.js"],
        "persistent": true
      }
    }
    

    【讨论】:

      【解决方案2】:

      试试setInterval()。您应该在后台页面中将您的逻辑包装在其中。如果您正在执行字符串,请不要忘记在您的 manifest.json 中添加 "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"。更多内容请浏览this

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        相关资源
        最近更新 更多