My addon should automatically process newly opened tabs, for example extract links. Therefore I need to get the DOM document. I tried different approaches but no one seems to work. The minimal example looks like this:
var { viewFor } = require("sdk/view/core"); //convert to low lvl
var mtabs_utils = require("sdk/tabs/utils");
var mtabs = require("sdk/tabs"); //all tabs across all windows
mtabs.open("http://www.stackoverflow.com");
mtabs.on('pageshow', newTabLoaded);
function newTabLoaded(tabHighLevel) {
console.log("v0: "+tabHighLevel.url);
tabHighLevel.activate(); //does not matter
//v1
var chromeWindow = viewFor(tabHighLevel.window);
console.log("v1: "+chromeWindow.content.location.href);
//v2
console.log("v2: "+mtabs_utils.getTabContentWindow(viewFor(tabHighLevel)).content.location.href);
//v3
console.log("v3: "+mtabs_utils.getTabBrowserForTab(viewFor(tabHighLevel)).contentDocument.location.href);
//v4
console.log("v4: "+mtabs.activeTab.url); //http://stackoverflow.com/questions/12609753/get-window-object-from-tab
console.log();
}
The output after
jpm run
is obviously
v0: http://stackoverflow.com/
v1: http://stackoverflow.com/
v2: http://stackoverflow.com/
v3: http://stackoverflow.com/
v4: http://stackoverflow.com/
and correct. After I open a new tab (middle mouse click "Questions") "newTabLoaded()" gets invoked again and the output is
v0: http://stackoverflow.com/questions
v1: http://stackoverflow.com/
v2: http://stackoverflow.com/
v3: http://stackoverflow.com/
v4: http://stackoverflow.com/
That means the passed parameter tabHighLevel is correct but the various conversions won't work at all. Furthermore the line
tabHighLevel.activate();
has no effect on the output. It only changes the focus in the actual browser automatically to the newly opened tab (correct behavior). This is odd, because if I open a new tab via middle mouse button and than select the new tab manually (in my opinion the same thing ".activate();" does), before the pageshow event fires and "newTabLoaded()" gets invoked, then the output is correct.
v0: http://stackoverflow.com/questions
v1: http://stackoverflow.com/questions
v2: http://stackoverflow.com/questions
v3: http://stackoverflow.com/questions
v4: http://stackoverflow.com/questions
I read something about that it is only possible to get the document of the active window to prevent security implications, but if that is true, than the call ".activate();" should be the appropriate workaround...
So, how can I get the document from the newly opened tab? Preferably without changing the actual selected tab.