After many Googling and almost giving up, I came up with the following solution. It uses the entered content script to send a message back to the extension after loading the correct page.
manifest.json:
{ ... "background": { "page": "background.html" }, "content_scripts": [ { "matches": ["http://website.com/index.php"], "js": ["content.js"], "all_frames": true, "run_at": "document_start" } ], "permissions": [ "*://*.website.com/*" ] }
background.html:
<html> <head> <script type="text/javascript" src="background.js"></script> </head> <body> <iframe src="about:blank"></iframe> </body> </html>
background.js:
var waiting = false; function login(){ // Call this function to start var frame = document.getElementsByTagName('iframe')[0]; frame.src = "https://website.com/login/"; waiting = true; } function callback(){ // This gets called once the page loads console.log("Done!"); } chrome.extension.onMessage.addListener(function(request, sender, sendResponse){ if(request.loaded && waiting){ // If you used a pattern, do extra checks here: // if(request.loaded == "https://website.com/index.php") document.getElementsByTagName('iframe')[0].src = "about:blank"; waiting = false; callback(); } });
content.js:
chrome.extension.sendMessage({loaded: window.location.href});
Dan hlavenka
source share