Chrome扩展:如何从内容脚本传递一个变量来background.html变量、脚本、内容、Chrome

2023-09-10 15:50:22 作者:初歌

我无法弄清楚如何从内容脚本传递变量(或变量数组)一个背景页。我想要做的就是找​​到一定的DOM元素与我的内容脚本,然后将它们发送给我的后台页面,这样我可以做一个跨域XMLHtt prequest与他们(它们存储在一个数据库上不同的网站)。

我的code是如下。我知道,没有被通过名为连载变量(基于我目前的code,我不希望它,但有它在那里,以便更容易地看到什么,我想要做的。)那么,怎样才能我通过从内容脚本变量为背景的网页?

我的内容脚本:

 函数onText(数据){
警报(伟大的成功!);
};
$(#insertprofile)。点击(函数(){
序列= $(certaininputs。)序列()。
chrome.extension.sendRequest({'动作':'insertprofile,序列化:连载},onText);
});
 

我的背景页面:

 <脚本>
      功能insertprofile(连载){
        VAR XHR =新XMLHtt prequest();
        xhr.onreadystatechange =功能(数据){
          如果(xhr.readyState == 4){
            如果(xhr.status == 200){
              VAR数据= JSON.parse(xhr.responseText);
              回调(数据);
            } 其他 {
              回调(空);
            }
          }
        }
        VAR URL =('http://mysite.com/storeindatabase.php?+连载);
        xhr.open(GET,URL,真正的);
        xhr.send();
      };
      函数onRequest(请求发送方,序列化){
        如果(request.action =='insertprofile'){
          insertprofile(连载);
        }
      };
      chrome.extension.onRequest.addListener(onRequest);
    < / SCRIPT>
 

解决方案

你缺少... PHP?序列='+连载)这个code:

  VAR URL =('http://mysite.com/storeindatabase.php?+连载);
 
新鲜出炉 2020 Chrome扩展程序性能报告

这应该是这样的:

  VAR URL =('http://mysite.com/storeindatabase.php?serialize='+连载);
 

I can't figure out how to pass a variable (or an array of variables) from a content script to a background page. What I'm trying to do is find certain DOM elements with my content script, then send them to my background page so that I can make a cross-domain XMLHttpRequest with them (store them in a database on a different site).

My code is below. I know that the variable named "serialize" is not being passed (and I don't expect it to based on my current code but have it in there so it's easier to see what I want to do.) So how can I pass a variable from a content script to a background page?

My Content Script:

function onText(data) {
alert("Great Success!!");
};
$("#insertprofile").click(function(){
serialize = $(".certaininputs").serialize();
chrome.extension.sendRequest({'action' : 'insertprofile', 'serialize' : serialize}, onText);
});

My Background Page:

   <script>
      function insertprofile(serialize) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(data) {
          if (xhr.readyState == 4) {
            if (xhr.status == 200) {
              var data = JSON.parse(xhr.responseText);
              callback(data);
            } else {
              callback(null);
            }
          }
        }
        var url = ('http://mysite.com/storeindatabase.php?' + serialize);
        xhr.open('GET', url, true);
        xhr.send();
      };
      function onRequest(request, sender, serialize) {
        if (request.action == 'insertprofile') {
          insertprofile(serialize);
        }
      };
      chrome.extension.onRequest.addListener(onRequest);
    </script>

解决方案

Did you missing "...php?serialize=' + serialize)" on this code:

var url = ('http://mysite.com/storeindatabase.php?' + serialize);

It's should be like this:

var url = ('http://mysite.com/storeindatabase.php?serialize=' + serialize);