Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in launching Chrome Extension for a specific page

I'm writing a simple Chrome extension that displays a JavaScript alert saying "Hello World". In this example I have specified that the extension will only run for google.com (by putting this in the permissions property within manifest.json).

Even after everything in the target page has loaded, the alert doesn't appear. Here is my script so far:

File: manifest.json

{   "name": "Hello",   "version": "1.0",   "description": "Says hello to Google",   "permissions": ["http://*.google.com/"]   "browser_action": {     "popup": "Hello.html"   } } 

File: Hello.html

<script language="Javascript">    alert("Hello World"); </script> 
like image 749
Noor Avatar asked May 01 '12 10:05

Noor


People also ask

Why is my Chrome extension not opening?

Re-enable Malfunctioning Extensions Open the three-dot menu from the top-right corner. Head to More tools > Extensions. Use the toggle for each extension to turn it off. Restart Chrome and go back to the extensions list.

How do you fix this page has been blocked by an extension?

Go to Help and select About Google Chrome. Check if you are running the latest version of Chrome. If not, click on the Update now button and wait for the installation to complete. Relaunch the browser and that should fix the webpage blocked by extension error.

How do I stop Chrome from blocking extensions?

Open Chrome and enter “ chrome://flags/ ” in the address bar, then hit “Enter.” This should take you to the advanced configuration section. To remove the “Extensions” menu button from the Chrome toolbar, toggle the dropdown list on the right and select “Disabled.”


1 Answers

You are adding a browser action popup, which adds a button to the top-right of your browser. (It's probably invisible because you haven't specified an image for it. There should be some empty space to the right of your address bar; try clicking it to see your Hello.html in a popup.)

What you want is a content script. Content scripts can get injected into every page that Chrome loads. You can use the matches and exclude_matches sub-items in your manifest file to specify which pages get your injected script.

{   "name": "Hello",   "version": "1.0",   "description": "Says hello to Google",   "permissions": ["tabs", "*://*.google.com/*"],   "content_scripts": [     {       "matches": ["*://*.google.com/*"],       "js": ["hello.js"]     }   ] } 

Make sure you rename Hello.html to hello.js (and get rid of the <script> tags).

Note also that I changed your http://*.google.com/ to *://*.google.com/* so that it will apply to Google over HTTP and HTTPS (and the trailing * ensures that it will apply to all pages on google.com, not just the main page).

like image 189
apsillers Avatar answered Oct 15 '22 01:10

apsillers