Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing the current html page from chrome extension

I'm new to chrome extensions. I would like to create a simple chrome extension that popup an alert with the title of the current html page. when I'm performing: alert(document.title) , I'm not getting it because the document object doesn't belong to the page but to the extension script (is it correct?) how do i get the right document object?

like image 394
DanielB Avatar asked Oct 08 '11 13:10

DanielB


People also ask

How do I check my current extensions in Chrome?

On your computer, open Chrome . At the top right, click Extensions . point to "This can read and change site data."

How do I view the source code of an extension?

After the extension is added to your browser, open an extension's page on the Chrome Web Store, such as the Markdown Preview extension page. When the page loads, click on the CRX icon in the extensions bar in Chrome and select “View source.”

Can I see source of chrome extension?

Open chrome://version/ and find the "Profile Path:` field. Open that folder up. All your extensions are here, with typically readable source.


1 Answers

Content scripts are the easiest way to go:

Expand your manifest file with this code:

...
"content_scripts": [
  {
  "matches": ["http://urlhere/*"],
  "js": ["contentscript.js"]
  }
],
...

Content script (automatically executed on each page as mentioned at matches at the manifest file):

alert(document.title)

The advantage of using content scripts over chrome.extension.* methods is that your extension doesn't require scary permissions, such as tabs.


See also:
  • Developer's guide
  • Content scripts
  • Background pages
like image 92
Rob W Avatar answered Oct 16 '22 06:10

Rob W