内容脚本/后台脚本乒乓通信头痛

时间:2017-03-08 21:29:29

标签: javascript iframe google-chrome-extension

在我的第一次扩展中,我试图在我自己的MySQL数据库中保存用户论坛的注释,这基本上可以正常工作。

现在我遇到了这个问题:我需要一些额外的信息,这些信息应该在后台收集,而不会让人感到不安。用户(相同的域,但HTTPS)。 我尝试在后台页面中使用iframe执行此操作,应该在iframe中加载一个页面,然后从那里收集一些信息,然后在iframe中加载另一个页面,并从那里收集一些其他信息我想出了这个:

内容脚本:提交按钮触发后台页面以在iframe中加载网址

背景页面:在iframe中加载网址,并向内容脚本发送消息,表明其已完全加载

内容脚本:接收消息,从iframe收集信息  并将其发送到后台页面。然后再一次同样的事情。最后通过$ post到我的服务器的背景页面信息。

事实上,这种乒乓球通讯并不适合我。 bg.js收到信息,但我无法将其发送回内容脚本。可能,我不知道哪个脚本准备好接收信息,何时内容脚本从活动选项卡获取信息以及何时从后台脚本中获取iframe信息。

我注意到内容脚本在重新加载扩展时从iframe获取信息。

清单:

{
  "manifest_version": 2,
  "name": "SaveMyCommenz",
  "version": "1.0",
  "description": "SMC",
  "version": "1",
  "browser_action": {
    "default_icon": "icons/icon-19.png",
    "default_popup": "popup.html"
  },

  "icons": {
    "48": "icons/icon-48.png",
    "96": "icons/icon-96.png"
  },

  "content_scripts": [
    {
      "matches": ["*://*.example.com/*"],
      "js": ["jquery.min.js", "smc.js"],
      "run_at": "document_end",
      "all_frames": true
    }
  ],

  "background": {
    "scripts": ["bg.js"]
  },

 "permissions": [
   "http://example.me/*",
   "https://example.me/*",
   "tabs"
 ]
}

smc.js(内容脚本)

// ################# MESSAGE PASSING ##########################
document.addEventListener("submit",function(){
         // tell bg.js to load site1 in iframe
         chrome.runtime.sendMessage('1');
        //...
        }


chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) {
  if (response == '2'){// get Info1 
    //get info1
    var link = document.querySelector("a[href*='permalink']");
    // send info1 to bg.js...
    //tell bg.js to load 2nd site in iframe
    chrome.runtime.sendMessage('3');
    }

    if (response == '4'){// get Info2 
    var link = document.querySelector("a[href*='permalink']");
    // send info2 to bg.js...
    }

});

bg.js(背景脚本)

load_iFrame();

function load_iFrame() {
    var iframeX = document.createElement("iframe");
    iframeX.setAttribute("src", "https://example.com");
    iframeX.style.width = "90%";
    iframeX.style.height = "500px";
    document.body.appendChild(iframeX);
}
// ################# MESSAGE PASSING ##########################
chrome.runtime.onMessage.addListener(function (response, sender, sendResponse) {
    //console.log("in bg.js angekommen: " + response.a);
    if (response == '1') {
        //chrome.runtime.reload(); // Kommentarseiten laden
        // load site 1
        window.frames['iframeX'].location = "https://example.com?page=1";
        // reload?
        // tell content script that site is fully loaded
        chrome.tabs.query({
            active: true,
            currentWindow: true
        }, function (tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {
                "2"
            });
        });
    }
    if (response == '3') {
        // load site 2
        window.frames['iframeX'].location = "http://example.com?page=2";
        // reload?
        // tell content script that site is fully loaded
        chrome.tabs.query({
            active: true,
            currentWindow: true
        }, function (tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {
                "4"
            });
        });
    }
});
var kom = document.querySelectorAll("a[href*='permalink']");
var i;
const kommentarSta = new Array;
for (i = 0; i < kom.length; i++) {
    kommentarSta[i] = kom[i].innerText;
    console.log("Kommentar" + i + ": " + kommentarSta[i]);
}

我何时以及如何重新加载哪个页面或应该进行哪些通信?

1 个答案:

答案 0 :(得分:0)

if (response.a == '1'){

load_iframe();  
//chrome.runtime.reload();      
window.frames['iframeX'].location = "https://derstandard.at/userprofil/postings?ortMode=1&groupMode=2&pageNumber=2";

        }


function load_iFrame() {
    var iframeX = document.createElement("iframe");
    iframeX.setAttribute("src", "https://derstandard.at/userprofil/postings?sortMode=1&groupMode=2&pageNumber=1");
    iframeX.style.width = "90%";
    iframeX.style.height = "500px";
    document.body.appendChild(iframeX);

}

bg.js应该等待来自内容脚本的message = 1,然后在iframe中加载新页面,但它没有。它说iframeX不存在。当我在bg.js中没有iframe之前重新加载时:/ 请求帮助

相关问题