I'm trying to create an extension using this docs:
http://code.google.com/chrome/extensions/content_scripts.html
I want a part of JS code to run when document is ready (loaded).
This is my manifest.json:
{
"name": "OwnExtension",
"version": "0.1",
"content_scripts": [
{
"matches": ["https://my.site.eu/*"],
"css": ["styles.css"],
"js": ["main.js"]
}
]
}
This is my main.js:
alert(10);
Am I doing sth wrong, that nothing happend when page https://my.site.eu/ loaded in browser?
alert() doesn't work from a content script.
Try console.log("hello") and you should see it in the developer panel when you view your extension in debug mode.
Related
I made custom chrome extension which loads external javascript file (file is hosted on amazon s3) using XMLHttpRequest. Code snippet which calls this XMLHttpRequest is in separate file, and included in manifest.json under content_scripts:
{
"background": {
"scripts": [ "background.js" ]
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": [ "http://*/*", "https://*/*" ]
} ],
"manifest_version": 2,
"name": "Name",
"short_name": "shortname",
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.0"
}
If I don't check inline install, it is accepted and published in few minutes.
But when I check inline install and choose site, review process takes a long time and extension becomes rejected. Domain is secured (https). And tag containing info about extension is added to site's head tag.
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/itemID">
So, the rejection reason is probably related to inline installation method, and there should be some trick? Anyone for help? Is there something wrong I did?
I'm trying to create a custom new tab page for Chrome. I have it in my manifest.json so that: "chrome_url_overrides": {
"newtab": "home.html"
}
It won't, however, load the scripts that I load in my home.html. I found out that you need to use content_scripts, so now I also have
"content_scripts": [
{
"matches": [""],
"css": ["style.css"],
"js": ["logic.js", "macros.js"]
}
]
What do I put in the "matches"? It doesn't allow "home.html" or "chrome://newtab".
I am trying to create a simple Google Chrome extension. I don't want it to have an icon next to the omnibar. Is this possible? What do I need to put in my manifest.json?
Yes, this is possible. To do it, just don't declare browser_action in your manifest. Remember, though, to get it to work, you'll need to do something to run the script. contextMenus are a great way to do that.
Here's a manifest that has no browser action line, so it will not display an icon:
manifest.json
{
"manifest_version": 2,
"description": "Example",
"name":"Example",
"icons": {
"16": "img16.png" }, // Needed for the context menu
"background": {
"scripts":["background.js"] },
"permissions":[ "tabs", "contextMenus" ], // contextMenus permission allows you to create the action
"version": "1.0"
}
Making a simple plugin , which executes javascript when a page is loaded , and I want to execute different javascript on different page
But the following code wasn't working as expected , no "alert" was triggered:
background.html:
<html><body>
<script>
chrome.webRequest.onCompleted.addListener(
function(details) {
alert (details.url);
},
{
urls: ["*"],
types: ["main_frame"]
},
[]
);
</script>
</body></html>
manifest.json:
{
"name": "JS",
"background_page": "background.html",
"permissions": [
"webRequest",
"*"
],
"version":"0.10"
}
Alerts and console.log made from the background page of an extension simply aren't visible on the general pages.
If you want to see them, you have to open the background page : Go to the extensions settings page (menu tools/extensions) and click the "background.html" link below the name of your extension.
In your case it may be better, during development phase, to simply add the console.log and alerts in the content scripts (i.e. not the background page). So you can read them without opening the background page.
EDIT : as requested, an extension that will simply alert the location :
main.js :
alert(document.location.href);
manifest.json :
{
"name": "Any name",
"version": "3.3",
"background_page": "background.html",
"content_scripts": [
{
"matches": ["http://*/*"],
"all_frames" : true,
"run_at" : "document_end",
"js": [
"main.js"
]
}
]
}
Yes I tested it. And yes it's as painful as it sounds. You should use console.log instead of alert during your dev.
I want to insert a javascript file into the original new tab page of chrome to be executed,
I tried this but error in matching when I'm trying to load the extension.
"content_scripts": [
{
"matches": ["chrome://newtab/*"],
"js": ["jquery.js", "myscript.js"]
}
]
You cannot do that, you need to use override pages to do this.
{
"name": "My extension",
...
"chrome_url_overrides" : {
"newtab": "myPage.html"
},
...
}
Add this to your matches: "matches": ["*://*.google.com/_/chrome/newtab*"]
That url looks like an implementation detail, so no guarantees it won't change, but for now it works.