1
在過去的幾天裏,我試圖構建Chrome擴展,它將注入按鈕到https頁面(done :))。一旦按鈕被按下,我會從DOM(完成:)讀取一些值,並將它們推送到test.php文件(這是問題),它將通過電子郵件將這些變量發送給em。Chrome擴展執行php文件
到目前爲止,我創造了這個:
的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('[email protected]', 'test', $message); ?>
所以,我能夠注入按鈕,讀取DOM值併發送郵件到發送郵件的test.php,但我無法傳遞我的數組。
你能告訴我如何傳遞數組嗎?
從content.js campaignInfo陣列通過background.js到test.php的 – rkc
大家好,另外2小時後,我發現了什麼是錯誤的, 固定上面的代碼我不得不修改如下:在content.js dataType:'JSON', data:{result:JSON.stringify(campaignInfo)} in php: <?php $ message = $ _POST [「result」]; mail('[email protected]','test',$ message);?> – rkc
@rkc如果你有一個工作解決方案,你應該把它作爲(自己)回答並接受它,這樣問題是不要留下。或者,如果您認爲對其他人沒有用處,則可以刪除該問題,但要小心,因爲重複這樣做會觸發問題禁止。 – Xan