Chrome扩展程序执行php文件

时间:2016-10-24 12:59:08

标签: php jquery ajax google-chrome-extension

在过去的几天里,我正在尝试构建Chrome扩展程序,它会将按钮注入https页面(完成:))。一旦按下按钮,我将从DOM(完成:)中读取一些值并将它们推送到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值并将帖子发送到发送电子邮件的test.php,但我无法通过我的数组。

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

1 个答案:

答案 0 :(得分:1)

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