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
615
votes
6 answers

Access variables and functions defined in page context using a content script

I'm learning how to create Chrome extensions. I just started developing one to catch YouTube events. I want to use it with YouTube flash player (later I will try to make it compatible with HTML5). manifest.json: { "name": "MyExtension", …
188
votes
4 answers

Chrome extension: accessing localStorage in content script

I have an options page where the user can define certain options and it saves it in localStorage: options.html Now, I also have a content script that needs to get the options that were defined in the options.html page, but when I try to access…
user476214
  • 1,881
  • 2
  • 12
  • 3
114
votes
6 answers

sendMessage from extension background or popup to content script doesn't work

I know that question has been repeatedly asked in different ways, but I tried to go through all the answers (hopefully I didn't miss anyone) and none of them worked for me. Here is my extension's code: manifest: { "name": "test", "version":…
Subway
  • 5,286
  • 11
  • 48
  • 59
76
votes
4 answers

Debugging Content Scripts for Chrome Extension

General Questions Hello! I'm delving into the world of Chrome Extensions and am having some problems getting the overall workflow down. It seems that Google has recently switched to heavily advocating Event Pages instead of keeping everything in…
66
votes
7 answers

Chrome extension content script re-injection after upgrade or install

After the Chrome extension I'm working on is installed, or upgraded, the content scripts (specified in the manifest) are not re-injected so a page refresh is required to make the extension work. Is there a way to force the scripts to be injected…
65
votes
2 answers

How to inject CSS using content script file in Chrome extension?

I'm trying to inject my CSS from JavaScript which is injected as content script: "content_scripts": [ { "matches": ["http://www.google.com/*"], "js": ["script.js"] } ], I found similar question about injecting CSS, but I…
Roman
  • 5,358
  • 9
  • 34
  • 43
64
votes
1 answer

Chrome extension code vs Content scripts vs Injected scripts

I am trying to get my Chrome Extension to run the function init() whenever a new page is loaded, but I am having trouble trying to understand how to do this. From what I understand, I need to do the following in background.html: Use…
Jon
  • 2,249
  • 6
  • 26
  • 30
60
votes
6 answers

Overriding !important with css or jquery

(This is using content scripts in a chrome extension) I need to overwrite some css properties that the webpage has labeled as !important. Is this possible? For instance, if I want to get rid of the border that is labeled…
Wilson
  • 8,570
  • 20
  • 66
  • 101
48
votes
3 answers

How to get a content script to load AFTER a page's Javascript has executed?

My extension is supposed to load a content script, searchTopic.js, only after the page it's injected into has already fully loaded (yes, I have set "run_at" to "document_end" in the extension manifest), but in fact it's loading before all the DOM…
FractalBob
  • 3,225
  • 4
  • 29
  • 40
44
votes
1 answer

How to postMessage into iFrame?

I am developing a Chrome extension to use with my own Web-application, processing Web-pages (including cross-domain ones), loaded into iFrame on my application's main page. So I have to send message, containing the URL of the page to be processed,…
37
votes
4 answers

How to use jquery in google chrome extension page action background.js?

I'm developing a "page action" google chrome extension. My manifest has: ... "background": { "scripts": ["background.js"] }, ... In my background.js file I have: function doSomething() { alert("I was…
User
  • 62,498
  • 72
  • 186
  • 247
36
votes
1 answer

Access window variable from Content Script

I have a Chrome Extension that is trying to find on every browsed URL (and every iframe of every browser URL) if a variable window.my_variable_name exists. So I wrote this little piece of content script : function detectVariable(){ …
François Pérez
  • 1,489
  • 2
  • 12
  • 12
31
votes
7 answers

Injecting multiple scripts through executeScript in Google Chrome

I need to programmatically inject multiple script files (followed by a code snippet) into the current page from my Google Chrome extension. The chrome.tabs.executeScript method allows for a single InjectDetails object (representing a script file or…
Douglas
  • 53,759
  • 13
  • 140
  • 188
30
votes
3 answers

Adding complex HTML using a Chrome content script

I am working with Chrome extension's content script to create a complex display that is added on web pages. I have first tested it directly integrated on a website, but now I need to put it in an extension. The thing is that the content script API…
28
votes
1 answer

Chrome content scripts aren't working: DOMContentLoaded listener does not execute

I am trying to code extension that corrects misspellings on 1 forum. I am trying to access

tag, with content script, but it doesn't change anything (using the code below): document.addEventListener("DOMContentLoaded", function() { …

1
2 3
47 48