Questions tagged [content-script]

Content scripts are JavaScript files, used by browser extensions or add-ons. They are the part of an extension that can interact with the document context of the current web page. This mainly applies to Firefox or Chrome extensions.

Content scripts are JavaScript files, used by browser extensions or add-ons. They are the part of an extension that can interact with the document context of the current web page. This mainly applies to Firefox or Chrome extensions.

Content scripts interact with a webpage via the DOM API, and they frequently have enhanced privileges and access to privileged browser-API functions compared to ordinary webpage javascript.

For this reason, content scripts are isolated from a target-page's javascript by scoping, and/or sandboxing, and/or anonymous function wrapping. Content-script javascript cannot (normally) directly interact with a page's javascript objects, and vice versa.

Content scripts are very similar to userscripts and Greasemonkey scripts, but as part of full-fledged extensions (add-ons), content scripts can have both more powerful options and more involved coding requirements.

References:


Related tags:

719 questions
27
votes
3 answers

Chrome extension adding external javascript to current page's html

I am adding some external JavaScript to the end of the page via my chrome extension. The external JavaScript then tries to post some data back to the server, however that is not taking place. The JavaScript wants to get the url of the current page…
25
votes
1 answer

Difference between background script and content script in chrome extension

As the questions says, I just want to know the difference between background script and content script in chrome extension. When I logged the chrome object in both the scripts, I found the different objects. Use case I want to inject my javascript…
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
24
votes
2 answers

what are content scripts in chrome inspector

While this could be a very basic thing, I can't seem to find an answer to this one. I see a lot of discussion about content scripts. When I open web inspector in chrome, and select sources, I see a tab called content scripts. I see a couple of…
Kiran
  • 5,478
  • 13
  • 56
  • 84
23
votes
2 answers

Chrome extension set to `run_at` `document_start` is running too fast?

EDIT: Something was wrong with my Chrome browser and creating a conflict with my script, a full reinstall eliminated whatever the problem source was. If I happen to find out what was causing it I will include it in here. EDIT2: Just to let anyone…
23
votes
3 answers

Chrome Extension Send Message From Background.js to Content Script

I have read the documentation on how to do Send Message From background javascript file(main.js) to Content Script (content.js) but I cannot get the onMessage to open my alert. Manifest.json { "name": "Example", "version": "1.0.1", …
Tyler Rafferty
  • 3,391
  • 4
  • 28
  • 37
21
votes
3 answers

Optionally inject Content Script

Content Script can be injected programatically or permanently by declaring in Extension manifest file. Programatic injection require host permission, which is generally grant by browser or page action. In my use case, I want to inject gmail,…
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
20
votes
2 answers

My CSS is not getting injected through my content script

Can anyone explain this to me. I'm trying to inject a CSS file onto a webpage using the content_script with Google extensions, but my css file never gets added to the webpage. Can someone tell me what I'm doing wrong and help me fix it?…
user1255276
  • 541
  • 2
  • 5
  • 20
19
votes
3 answers

Including Javascript files in a Chrome extension content script

I'm writing a Chrome extension, and want to write one JS file, that provides several expected functions that aren't present in another, then load that other file. I'm after behavior similar to require in Perl, #include in C, or execfile in Python…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
19
votes
1 answer

Chrome extension is not loading on browser navigation at YouTube

Let's say I have an extension that loads when you arrive at a YouTube video page.I have noticed that when one navigates back and forth using the Chrome buttons, the extension most probably won't load. As an example, I have 2 files, the manifest: { …
abinop
  • 3,153
  • 5
  • 32
  • 46
18
votes
1 answer

Chrome: get URL of active content script tab

I have a content script that is run when the user connects to "*://www.youtube.com/*". I need to know when the URL changes, as YouTube does not open a new 'page', so-to-speak, when you click on a link; rather it changes the URL and page contents (I…
otoomey
  • 939
  • 1
  • 14
  • 31
17
votes
1 answer

Chrome extension: Communication between content script and background.html

I am new to Chrome extensions. I am trying to communicate between the content script and the background.html page. The background.html sends a request, "hello", to the content script and the content script should respond back with "hello background"…
Rebooting
  • 2,762
  • 11
  • 47
  • 70
16
votes
4 answers

My injected