如何将数据从chrome.tabs.executeScript发布到PHP?

时间:2019-05-22 21:54:00

标签: javascript google-chrome-extension

我正在尝试从chrome.tabs.executeScript类型的文件中获取结果并将其发布到PHP文件中。

这是我的方法:

**Popup.js Source Code**

// Here I have initiated the tab listener to get the source of the page.

chrome.runtime.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {
    message.innerText = request.source;
  }
});

//在这里,我正在执行文件getPagesSource.js并将数组响应保存在htmlout中。

function onWindowLoad() {
      var message = document.querySelector('#message');
      chrome.tabs.executeScript(null, {
        file: "getPagesSource.js"
      }, htmlout);



jQuery.ajax({
    url: "http://gggg.com/core/backend/connect.php",
    data:{ htmlout: htmlout }, 
    type: "POST",
    success: function(data) {
      console.log(data);
      htmlout = [];
    },
    error: function (){}
  });
}

// Executing Script
window.onload = onWindowLoad;

结果是:程序未运行。没有反应。只是#message“插入脚本.....”中的默认消息

**getPagesSource.js Source Code**

function DOMtoString(document_root) {
    var html = '',
        node = document_root.firstChild;
    while (node) {
        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
            html += node.outerHTML;
            break;
        case Node.TEXT_NODE:
            html += node.nodeValue;
            break;
        case Node.CDATA_SECTION_NODE:
            html += '<![CDATA[' + node.nodeValue + ']]>';
            break;
        case Node.COMMENT_NODE:
            html += '<!--' + node.nodeValue + '-->';
            break;
        case Node.DOCUMENT_TYPE_NODE:
            // (X)HTML documents are identified by public identifiers
            html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
            break;
        }
        node = node.nextSibling;
    }


    var fg = html;




    return fg;  



}





chrome.runtime.sendMessage({
    action: "getSource",
    source: DOMtoString(document)
});

Popup.html源代码

<!DOCTYPE html>
<html style=''>
<head>


<script src='popup.js'></script>
</head>
<body style="width:400px;">
<div id='message'>Injecting Script....</div>
</body>
</html>

0 个答案:

没有答案