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
13
votes
3 answers

Call background function of Chrome extension from a site

I am looking for a function inside a webpage te activate a chrome extension. Imagine that http://www.example.com/test.html contains: And my background page contains the definition of the hello function: function hello()…
13
votes
1 answer

Support all google domains in a content script

I am making a content script that does something with the google results webpage. The line below in the manifest.json is not valid. "matches": [ "https://www.google.*/*" ] The error due to above line in manifest.json is: Invalid value for…
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
12
votes
3 answers

What exactly are content script and background script in the Chrome extension?

I am working on a Chrome extension to auto-fill the demo data. I have created the JavaScript file xyz.js for auto-filling the data. I am executing the script by clicking on the button with chrome.tabs.executeScript(tabs[0].id, {file: "xyz.js"}); and…
12
votes
2 answers

$x() function is not defined inside a Chrome extension, content script

$x("//a[contains(@href,'.jpg')]"); works as expected from the developer tools command prompt. But, when in an extension's content-script I get a '$x is not defined'. Why is this not available in a content-script or is there a special way of…
12
votes
3 answers

How to to initialize keyboard event with given char/keycode in a Chrome extension?

I'm developing a Google Chrome extension which simulates keyboard events on a web-page. I found that event.initKeyboardEvent() does not work properly because of this webkit bug and I also found some workarounds, e.g. SO Question However, defining…
11
votes
2 answers

In chrome extension, how to use content script to inject a Vue page

I'm trying to implement a JSON viewer chrome extension. I already have the Viewer implemented with Vue (http://treedoc.org). Now the problem is how can I can inject the Vue page with Chrome extension content script. I found this post is very…
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
11
votes
1 answer

How to load a content script on all Google (international) pages?

For a Google-Chrome extension, I would like to load a content script on all Google pages. What is the best way to do this? I tried this, in the manifest.json, but it does not work: "matches": ["http://www.google.*/*",…
11
votes
1 answer

How to trigger a Chrome extension, content-script for a given hash in the URL?

My matches scheme: "content_scripts" : [ { "matches" : [ "https://stackoverflow.com/questions#epic*" ], "js" : ["silly.js"] } ], So if the user went to a webpage (like https://stackoverflow.com/questions) then added #epic it would…
Alex
  • 1,035
  • 3
  • 16
  • 31
11
votes
2 answers

Adding @font-face stylesheet rules to chrome extension

What is the recommended way to add a @font-face stylesheet rule through a chrome-extension? The problem is that the url of the font embed is located from within the extension, so I must do it in javascript in order to use chrome.extension.getURL. I…
badunk
  • 4,310
  • 5
  • 27
  • 47
11
votes
1 answer

Hijacking a variable with a userscript for Chrome

I'm trying to change the variable in a page using a userscript. I know that in the source code there is a variable var smilies = false; In theory I should be able to change it like that: unsafeWindow.smilies = true; But it doesn't work. When I'm…
Badr Hari
  • 8,114
  • 18
  • 67
  • 100
9
votes
1 answer

Alternatives to Bootstrap JS does not work inside shadow dom

I tried to create a shadow DOM for encapsulating the content Script elements and then apply Bootstrap styles and also make Bootstrap Modal work inside a shadow Dom so it can encapsulate itself from the Webpage styles and Scripts. It is successful…
vba
  • 526
  • 8
  • 20
9
votes
1 answer

chrome.runtime.sendMessage throws exception from content script after reloading Chrome Extension

I send messages from the injected content scripts back to my background script in my Chrome Extension as such: chrome.runtime.sendMessage({action: "myResult"}); This works fine, until I reload my extension (by going to Settings -> Extensions ->…
c00000fd
  • 20,994
  • 29
  • 177
  • 400
9
votes
1 answer

chrome extension to Send Message from popup to content script

I,am developing an extension in which i have to extract data from linkedin profile page when user press button on popup. I,am passing message from the popup.js page to contentscript and in response i will get data extracted from linkedin profile…
9
votes
1 answer

how to make on/off buttons/icons for a chrome extension?

I want to let the user decide when they want to run a script, so that when the browser opens, the "off" icon is showing and no script runs; but when the user clicks it, it changes to an "on" icon and executes a userscript, until the user clicks off.…
8
votes
1 answer

Configurable keyboard shortcut without using content scripts

Chrome Extension: I am looking for a way to assign global keyboard shortcuts that can be invoked even when the current tab has no content (and hence no content script). Some examples of such tabs: 'new tab' tabs, chrome://extensions tabs, 'page not…
1 2
3
47 48