Reinsbrain
Reinsbrain

Reputation: 2591

Automated conditional browsing in background with chrome extension

I am researching the possibility that I might be able to use a Chrome extension to automate browsing and navigation (conditionally). My hope is that the extension can load a remote page (in the background) and inject a javascript to evaluate clickable links and click (by calling the click method) the appropriate (evaluated by some javascript logic) link, then repeat process for the resulting page.

My ability to javascript is not the problem - but I am struggling to discern whether (or not) a chrome extension can load pages in the back and inject script into them (making the DOM accessible).

I would be pleased if anyone could confirm (or deny) the ability to do so - and if so, some helpful pointers on where I should research next.

@Rob W - it seems the experimental features fit the bill perfectly. But my first tests seem to show the features are still very experimental ... ie. no objects get returned from callbacks:

background.html

function getAllosTabs(osTabs){
 var x = osTabs;
 alert(x.length); // error: osTabs is undefined
}

function createOffScreenTabCallback(offscreenTab){
 document.write("offscreen tab created");
 chrome.experimental.offscreenTabs.getAll(getAllosTabs);
 alert(offscreenTab); // error: offscreenTab is undefined
}

var ostab = chrome.experimental.offscreenTabs.create({"url":"http://www.google.com"}, createOffScreenTabCallback)
alert(ostab); // error: ostab is undefined

Some further digging into the chromium source code on github revealed a limitation creating offscreenTab from background:

Note that you can't create offscreen tabs from background pages, since they don't have an associated WebContents. The lifetime of offscreen tabs is tied to their creating tab, so requiring visible tabs as the parent helps prevent offscreen tab leaking.

So far it seems like it is unlikely that I can create an extension that browses (automatically and conditionally) in the background but I'll still keep trying - perhaps creating it from script in the popup might work. It won't run automatically at computer startup but it will run when the browser is open and the user clicks the browseraction.

Any further suggestions are highly welcome.

Upvotes: 0

Views: 1199

Answers (1)

Silviu-Marian
Silviu-Marian

Reputation: 10927

Some clarifications:

  • there's no "background" tabs except extension's background page (with iframes) but pages loaded in iframes can know they are being loaded in frames and can break or break at even the best counter-framebreaker scripts
  • offscreenTab is still experimental and is very much visible, as its intended use is different from what you need it for
  • content scripts, and chrome.tabs.update() are the only way handle the automated navigation part; aside being extremely harsh to program, problems and limitations are numerous, including CSP (Content-Security-Policy), their isolated context isolating event data, etc.

Alternatives... not many really. The thing is you're using your user's computer and browser to do your things and regardless of how dirty they are not, chrome's dev team still won't like it and will through many things at you and your extension (like the v2 manifest).

You can use NPAPI to start another instance chrome.exe --load-extension=iMacros.

Upvotes: 1

Related Questions