2

I'm trying to make a chrome extension that reloads an iframe of a site in the background every x seconds. This site requires login but as long as a user is active on the site, it doesn't log you out. I'm trying to get some information from the HTML of the site. However, when I load the iframe in the background script with the url, it redirects the iframe to a different url. I'm guessing it is because the iframe in the background doesn't work like opening the url in the actual chrome browser(When I loaded an iframe with the same url in a content script, the exact url loaded perfectly). Does anyone have any suggestions on how I could load a background iframe with a url that automatically redirects it(which I don't want)?

manifest.json

{
 "name":"Actual IC Notifications",
 "version": "1.2",
 "manifest_version":2,
 "description":"Short description",
    "permissions": ["<all_urls>"],
 "browser_action":{
  "default_icon":"bell.png",
  "default_popup":"popup.html"
  
 },
    
    "background":{
            "all_frames": true,
        "scripts":["background.js"]
     
    }


}

background.js

console.log("background is running");
window.onload = function(){
    var frame = document.createElement('iframe');
    frame.src = 'https://philasd.infinitecampus.org/campus/nav-wrapper/student/portal/student/today';
    document.body.appendChild(frame);

    window.addEventListener('message',function(e){
    console.log("message received: " + JSON.stringify(e.data));
});
    console.log('posting message to iframe');
    
    frame.addEventListener('load',function(){
        frame.contentWindow.postMessage("TestMessage","*");
    });
};

Background script console errors

Console output:

miselking
  • 3,043
  • 4
  • 28
  • 39
  • As per [SO post](https://stackoverflow.com/questions/22194409/failed-to-execute-postmessage-on-domwindow-the-target-origin-provided-does) with the same error, the error was triggered when your app is posting to a window that was not loaded completely. Also, it could be that you are using an iframe that has the sandbox attribute and `allow-same-origin` isn't set. – MαπμQμαπkγVπ.0 Oct 01 '18 at 09:58
  • @MαπμQμαπkγVπ.0 I tried both methods that you mentioned but I'm still getting the same two errors. I even took out all my code except the code that created the iframe and the iframe's properties. I think the src url has something to do with the errors. – David Zhang Oct 02 '18 at 01:14

0 Answers0