Chrome扩展执行php文件


Chrome extension to execute php file

在过去的几天里,我试图构建Chrome扩展,这将注入按钮到https页面(完成:))。一旦按钮被按下,我将从DOM中读取一些值(done:))并将它们推送到test.php文件(这就是问题所在),该文件将通过电子邮件将这些var发送给em。

到目前为止,我创建了这个:

manifest.json

{
"name" : "test",
"version": "1.0.1",
"manifest_version": 2,
"permissions":  [
                    "tabs", "activeTab", "https://www.googleapis.com/*"
                ],
"browser_action":   {
                        "default_icon": "icon128.png"
                    },
"content_scripts":  [
                      {
                        "js": ["jquery-3.1.1.min.js", "content.js"]
                      }
                    ],
  "background":   {
                      "scripts": [ "jquery-3.1.1.min.js", "background.js" ]
                  }}

content.js

(function(window, $, undefined){  

$('#sendbutton').on('click', function(event) { 

    var campaignInfo = {
        'name' : 'test_name',
        'surname' : 'test_surname'
    };
    var datastring = JSON.stringify(campaignInfo);
    chrome.runtime.sendMessage({
    method: 'POST',
    action: 'xhttp',
    url: 'http://localhost:8080/test/test.php',
    data: datastring
}); 
    console.log(campaignInfo);

  }); })(window, jQuery);

background.js

chrome.runtime.onMessage.addListener(function(request, sender, callback) {if (request.action == "xhttp") {    $.ajax({
    type: request.method,
    url: request.url,
    data: request.data,
    success: function(responseText){
        callback(responseText);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        callback();
    }
});
return true; }});

test.php

<?php if (isset($_POST["data"]) && !empty($_POST["data"])) { $message = $_POST["data"];} mail('testemail@gmai.com', 'test', $message); ?>

所以,我能够注入按钮,读取DOM值和发送post到test.php发送电子邮件,但我不能传递我的数组。

你能告诉我如何传递数组吗?

又过了2h,我发现了什么是错的,为了修复上面的代码,我必须修改如下:在content.js中dataType: 'JSON', data: {result:JSON.stringify(campaignInfo)} in php: