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?
On your computer, open Chrome . At the top right, click Extensions . point to "This can read and change site data."
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.”
Open chrome://version/ and find the "Profile Path:` field. Open that folder up. All your extensions are here, with typically readable source.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With