chrome.tabs.executeScript document_start means ?

1,611 views
Skip to first unread message

johnjbarton

unread,
Aug 1, 2012, 2:38:08 PM8/1/12
to chromium-...@chromium.org
In the documentation page for content scripts,
it says:
    "In the case of "document_start", the files are injected after any files from css, but before any other DOM is constructed or any other script is run. "
However, if I write to console.log in my JS from a script tag in the page, then from the executeScript callback, the script tag runs first.

My code is
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  // TODO test tab.url
  console.log("onUpdated sending executeScript for "+tab.url, changeInfo);
  var code = "console.log('before '+window.StandaloneTestRunnerURL);\n";
  code += "window.StandaloneTestRunnerURL='chrome-extension://fkhgelnmojgnpahkeemhnbjndeeocehc/atopwi/atopwi.html'\n";
  code +=  "console.log('after '+window.StandaloneTestRunnerURL);\n";
  chrome.tabs.executeScript(tabId, {
      code: code,
      allFrames: true,
      runAt: "document_start"
    },
    function onExecuteScript(){
      console.log("executeScript ");
    });
});

Any hints?

jjb

johnjbarton

unread,
Aug 1, 2012, 2:55:50 PM8/1/12
to chromium-...@chromium.org
Nevermind. I discovered you can't have a page open an extension page:

If I could have used window.open() on a chrome-extension URL then I could connect the popup to the window scripts with postMessage() because I would have the window object from window.opener.  Without this I would have to send messages from the page to content script to background to popup. But I will just work on something else, that's too silly.

jjb

Nathan Wray

unread,
Aug 1, 2012, 2:59:48 PM8/1/12
to johnjbarton, chromium-...@chromium.org
the pattern you're describing where a request goes via the content script to the background page is extremely common.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/-/Ngd7svTMUWcJ.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

John J Barton

unread,
Aug 1, 2012, 3:22:43 PM8/1/12
to Nathan Wray, chromium-...@chromium.org
Thanks, good to know. I've done a lot of it myself, but it's a huge time sink. I was really attracted to the idea of using chrome extensions as a way of building client-side Web applications without the need for web servers. But I keep running in to issues that make the extension platform advantage smaller and smaller.  Do I do the postMessage() dance again or just use a web server? I think its time to take the path more traveled.

jjb

Nathan Wray

unread,
Aug 2, 2012, 7:31:56 AM8/2/12
to eriq, chromium-...@chromium.org
I've never seen a case where the DOM in the target page is already loaded when your script runs at document_start.  We're injecting a script node into the target page so that when their DOM is parsed our script is running in their JVM context. 

If their DOM were already parsed when our script ran this would fail, and (knock on wood) I've never seen it fail.  We're on Chromium 19 though (1085), YMMV on other revisions I suppose.


On Wed, Aug 1, 2012 at 2:47 PM, eriq <eriq.au...@gmail.com> wrote:
The "runAt" option specifies the earliest time that the script injection can happen.
So, if you inject at "document_end", then you can be guaranteed that the DOM will be complete.

Injecting at "document_start" essentially means that the script will get injected at the first possible opportunity (maybe while the DOM is still loading).
It is pretty common for small DOMs to get loaded before the script has an opportunity to get injected.

-Eriq


On Wednesday, August 1, 2012 11:38:08 AM UTC-7, johnjbarton wrote:

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.

Eric Williams

unread,
Aug 2, 2012, 7:44:04 PM8/2/12
to eriq, chromium-...@chromium.org
On 8/1/2012 1:47 PM, eriq wrote:
> The "runAt" option specifies the earliest time that the script injection
> can happen.
> So, if you inject at "document_end", then you can be guaranteed that the
> DOM will be complete.
>
> Injecting at "document_start" essentially means that the script will get
> injected at the first possible opportunity (maybe while the DOM is still
> loading).
> It is pretty common for small DOMs to get loaded before the script has
> an opportunity to get injected.

My understanding is the behavior here is a bit different between a
declared content script and .executeScript, in that for a declared
content script, it'll run exactly when stated, whereas .executeScript
you're specifying it'll run "when stated, or now if that already happened"

John J Barton

unread,
Aug 2, 2012, 7:57:39 PM8/2/12
to Eric Williams, eriq, chromium-...@chromium.org
When are you folks calling executeScript(). I'm calling it from tabs. chrome.tabs.onUpdated handler. Is there another way?

jjb

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
Reply all
Reply to author
Forward
0 new messages