Extension to call javascript on window load

7,313 views
Skip to first unread message

dilip kumar

unread,
Sep 3, 2013, 4:03:46 PM9/3/13
to chromium-...@chromium.org
Hello,

I have a bookmarklet which calls javascript when clicked upon and logs the http request made by user on my server. I wanted to create a plugin/extension which does this automatically without user intervention when using the bookmarklet. Can somebody please point me on how to go about this automation of javascript code when window loaded event is called?

Thanks,
Dilip

Adam Barth

unread,
Sep 3, 2013, 8:30:32 PM9/3/13
to dilip kumar, Chromium-extensions
You might be interested in content scripts:


They're scripts that you include with your extension that are similar to bookmarklets.

Adam



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/ea41e692-b686-44a9-b4c2-7ca2f23c8f5e%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

dilip kumar

unread,
Sep 4, 2013, 4:49:10 PM9/4/13
to chromium-...@chromium.org, dilip kumar
Hi Adam,

I looked into ContentScripts and tried using it on DOM content loaded event listener but nothing happens. I have posted the manifest and JS code below. Please let me know where I am doing wrong.

{
  "manifest_version": 2,

  "name": "YouTube Logger",
  "description": "This extension logs YouTube requests.",
  "version": "1.1",

  "permissions": [
    "tabs", "http://www.youtube.com/*", "https://www.youtube.com/*"
  ]


document.addEventListener('DOMContentLoaded', function () {
        chrome.tabs.executeScript(null,
      {file:"simple.js"});
});


The file simple.js is just an alert message.


On Tuesday, September 3, 2013 8:30:32 PM UTC-4, Adam Barth wrote:
You might be interested in content scripts:


They're scripts that you include with your extension that are similar to bookmarklets.

Adam

On Tue, Sep 3, 2013 at 1:03 PM, dilip kumar <dilip...@gmail.com> wrote:
Hello,

I have a bookmarklet which calls javascript when clicked upon and logs the http request made by user on my server. I wanted to create a plugin/extension which does this automatically without user intervention when using the bookmarklet. Can somebody please point me on how to go about this automation of javascript code when window loaded event is called?

Thanks,
Dilip

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Adam Barth

unread,
Sep 4, 2013, 5:11:43 PM9/4/13
to dilip kumar, Chromium-extensions
There's no need to listen for the event.  Your content script starts executing shortly after DOMContentLoaded.


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

dilip kumar

unread,
Sep 4, 2013, 5:16:22 PM9/4/13
to chromium-...@chromium.org, dilip kumar
Thanks Adam. I changed to manifest to include ContentScripts and a simple alert code. But, I do not see the pop up when I access a webpage. Here is the manifest and the JS code


{
  "manifest_version": 2,

  "name": "YouTube Logger",
  "description": "This extension logs YouTube requests.",
  "version": "1.6",

  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["jquery.js","simple.js"]
    }
  ]

}


jquery.js is latest jquery production code.
simple.js is alert("Hello World")

Please help me understand why I do not see pop up when DOMContentLoaded event is triggered.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Adam Barth

unread,
Sep 4, 2013, 5:19:29 PM9/4/13
to dilip kumar, Chromium-extensions
Your code is running after the DOMContentLoaded event fires.

Adam



To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

dilip kumar

unread,
Sep 4, 2013, 5:25:14 PM9/4/13
to chromium-...@chromium.org, dilip kumar
But I do not see the pop up with "Hello World" message. Am I missing something?

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

dilip kumar

unread,
Sep 4, 2013, 5:30:54 PM9/4/13
to chromium-...@chromium.org, dilip kumar
Sorry, Its working now. Thanks.
How do I change the event on which the JS is called?

PhistucK

unread,
Sep 4, 2013, 6:30:57 PM9/4/13
to dilip kumar, Chromium-extensions
You can add "run_at": "document_start" to the content script definition (at the same level of "js" and "matches").


PhistucK


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

dilip kumar

unread,
Sep 5, 2013, 9:20:45 AM9/5/13
to chromium-...@chromium.org, dilip kumar
The extension works for initial page load, but when I click on an image, which loads another page on the same window the JS is not executed again. Can you please help me how to go about calling JS every time a new page is loaded in the same window?

Thanks!


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Adrian Aichner

unread,
Sep 5, 2013, 9:28:32 AM9/5/13
to dilip kumar, Chromium-extensions
How about run_at: "document_idle"


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
Reply all
Reply to author
Forward
0 new messages