0

This is my first time messing around with extensions and what I am trying to do is very simple yet I can't seem to get it to work.

I simply want an alert to be called every time a page on google is loaded.

In my manifest.json I have:

{
"name": "Bypass shib",
"version": "1.0",
"content_scripts": [
  {
  "matches": ["http://www.google.com/*"],
  "js": ["secondScript.js"]
}
],
"manifest_version": 2
}

Okay now in my secondScript.js I have:

chrome.tabs.executeScript(null, {code: "alert('test')"});

Shouldn't this execute the alert whenever a page is loaded? If not can somebody explain why it's not?

Eric Smith
  • 1,336
  • 4
  • 17
  • 32

2 Answers2

4

The console reveals the following message:

Uncaught Error: "executeScript" can only be used in extension processes.
See the content scripts documentation for more details.

This post suggests "Chrome extension functions cannot be used in content scripts," which could be what you're running into.

For completeness, the secondScript.js that worked for me was as follows:

console.log("test");
//chrome.tabs.executeScript(null, {code: "alert('test')"});
alert("test");
Community
  • 1
  • 1
Beyamor
  • 3,348
  • 1
  • 18
  • 17
  • Oh, and to share a face-palm piece of advice, make sure your manifest uses https if the url you're looking at uses https. – Beyamor Apr 08 '12 at 08:00
  • this does not work. The script gets executed only if you click on the extension and not on page load – strix25 Aug 21 '22 at 19:43
1

Content scripts do not have access to any of the chrome.tabs.* API methods.

To display an alert on every page, remove the chrome.tabs.executeScript method, and let your secondScript.js just contain:

alert('Test');

In a Chrome extension, there are three different kinds of scopes in which JavaScript can run. The understanding of this separation is essential for writing Chrome extensions, see this answer.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678