I'm trying to develope a chrome extension for the following task but get stuck: There is a Webpage that updates a Url aperiodically, and I want the extension to check for update regularly in the background and save new Url if updated. The problem is that the Url is generated by a remote javascript on that Webpage's domain in the form like:
<p> Here's the Url: <script type="text/javascript" src="http://www.****.com/?***=***"></script> </p>
and the script is simply
document.write("the_Url")
I cannot run that javascript in background page due to chrome's security policy so I think I have to open that Webpage and get the Url somehow from there.
So in my background page, I periodically call this
chrome.tabs.create({url:"Webpage_url", active:focusing}, function(tab) {
chrome.tabs.executeScript(tab.id, {file:"getUrl.js"});
});
and my getUrl.js is like:
$(document).ready(function(){
var write = document.write;
//overwrite document.write to save Url
document.write = function(Url)
{
//code here to save Url, for debug I tried just
console.log(Url);
}
//code here to parse html to get js_url: "http://www.****.com/?***=***", no id in original html, but definitely no problem here
$.getScript(js_url).done(function (s, state)
{
//after js run, set document.write back
document.write = write;
});
});
I have to do it this way because I cannot get the javascript in plain text by jquery ajax, it gives me: No 'Access-Control-Allow-Origin' header is present.
However using above method I still get warning: "Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." It's like the overwrite of document.write doesn't work, but actually if I put a "document.write("something")" just above the $.getScript part, I can successfully save "something". So it seems like the overwrite just doesn't affect the remote script.
I even tried removing the "document.write = write" redefination but still the same warning.
Anyone knows why? Or any other walkaround to achieve the original task is fine. Thank you.