Injected script was not load - google-chrome-extension

manifest.json
{
"manifest_version": 3,
"name": "Inject Scripts",
"version": "1.0",
"content_scripts": [{
"matches": [
"*://*/*"
],
"run_at": "document_end",
"js": [
"content.js"
]
}],
"web_accessible_resources": [{
"resources": [
"every site .js"
],
"matches": []
}]
}
content.js
document.head.insertAdjacentHTML('beforeend', ˋ<script src="${chrome.runtime.getURL('every site .js')}"></script>ˋ)
every site .js
console.log('injected')
<script> was inserted to <head> successful with the src="chrome-extension://<extension ID>/every site .js" attribute but there is neither console.log output nor Error message.
I installed my extension locally by enabling "Developer mode" and "Load unpacked" in "chrome://extensions/".
Do I need to specify "permissions" in manifest.json?

Related

Not running at document_start

My extension doesn't run at document_start. The manifest.json:
{
"manifest_version": 3,
"name": "Lift Web Restrictions",
"description": "Simple extension that removes most ads, web restrictions, adds mods to MooMoo.io / Krunker.io / and more!",
"version": "0.0.5",
"icons": {
"16": "logo/sw16.png",
"48": "logo/sw48.png",
"128": "logo/sw128.png"
},
"permissions": [],
"host_permissions": [
"*://*/*"
],
"background": {
"service_worker": "service-worker.js"
},
"web_accessible_resources": [
{
"resources": ["modules/script.js"],
"matches": ["*://*/*"]
}
],
"content_scripts": [{
"js": [
"modules/transfer.js"
],
"matches": ["http://*/*", "https://*/*"],
"all_frames": true,
"run-at": "document_start"
}]
}
There's a console.log within modules/transfer.js, however the page's script tag beats it.
Launching egAps
index.bf049c93.js?52f6f7bb66295262766e:2 Downloading d.js...
index.bf049c93.js?52f6f7bb66295262766e:2 Loading WASM c66eb3cbd8fdc1325900.wasm
^^^^ logged before script runs
index.bf049c93.js?52f6f7bb66295262766e:2 1 dependencies left
transfer.js:1 fireAt <--- where it runs
index.bf049c93.js?52f6f7bb66295262766e:2 0 dependencies left
index.bf049c93.js?52f6f7bb66295262766e:2 Running...
index.bf049c93.js?52f6f7bb66295262766e:2
Replace run-at with run_at
Thank you wOxxOm

google chrome extensions development

I am trying to develop my first extension in google chrome and I am following this tutorial: Tutorial
Everything is working great except the 2 lines of code in content.js
The problem is that the contents of "content.js" are not correct and I am getting "undefined" in chrome console for line 2.
Can someone please help me out and let me know why this happens?
I share the contents of content.js with you in order to help me out.
content.js contents:
var firstHref = $("a[href^='http']").eq(0).attr("href");
console.log(firstHref);
manifest/json file contents:
{
"manifest_version": 2,
"name": "My Cool Extension",
"version": "0.2",
"icons": { "128": "icon_128.png" },
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery-2.1.3.min.js", "content.js"]
}
],
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs"
]
}
I guess, your content script is loading before the whole document is loaded.That's why it returned undefined.
To make sure your content scripts are inject at properly, you should define "run_at" in file manifest.json
Example:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_end",
"all_frames": true
}
],
please check Content Scripts parameters for more details.

Chrome manifest "run_at": "document_end" not working when navigating

I have the following manifest to run on every page load.
The problem is that it is running only when refreshing the page.
The myscript.js will not work when I click on a link and navigate, again, only on refresh.
Why is that?
Thanks
{
"name": "BrowserActionExtension",
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_title": "That's the tool tip",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": [ "myscript.js" ],
"run_at": "document_end"
}
],
"permissions": [
"tabs",
"<all_urls>"
]
}
If the page contains frames and the link on which you click does not belong to the top frame you get this behaviour.
Take a look to all_frames
This value is always false, but if you set this to true the script is injected in all frames.

Content Security Policy doesn't allow mixed content

I am trying to access an internal machine of my network via chrome extensions. I am calling a php file from my chrome extension. I am using manifest file as follows:
{
"content_scripts": [ {
"js": [ "lib/jquery.js", "lib/jquery-ui.js", "lib/util.js" ],
"matches": [ "*://plus.example.com/*", "*://tools.example.com/*" ]
}, {
"js": [ "tools/g_tool.js" ],
"matches": [ "*://plus.example.com/*" ]
}, {
"js": [ "tools/b_tool.js" ],
"matches": [ "*://tools.google.com/*" ]
} ],
"icons": {
"128": "images/icon_128.png",
"16": "images/icon_16.png",
"48": "images/icon_48.png"
},
"manifest_version": 2,
"name": "Tool",
"permissions": [ "*://plus.example.com/*", "*://tools.example.com/*", "http://myusername-example.com/*"],
"version": "0.7",
"web_accessible_resources": [ "images/loading.gif" ]
}
The internal machine which I am using can be used only via http. So, I am looking around for a way to use it in my chrome extension.

Click events not reported to Chrome extension for Gmail pages

My extension is simply performing actions on pages based on user clicks; it works fine on all websites except for Gmail.
Here's my manifest file:
{
"name": "My Extension",
"version": "2.0",
"description": "description.",
"permissions": [
"tabs"
],
"icons": {
"16" : "images/icon-16.png",
"48" : "images/icon-48.png",
"128" : "images/icon-128.png"
},
"background_page": "background.html",
"options_page": "Options.html",
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["css/style.css"],
"js": ["inject.js"]
}
]
}
When I click on a web page, my inject.js normally fires an alert() -- it handles onclick events. But, on Gmail pages, it does not catch the click event.
Gmail is composed of frames. By default, content scripts are only injected at the top-level frame. Add "all_frames":true to the manifest, so that the content script is also injected in the frames.
"content_scripts": [{
"matches": ["<all_urls>"],
"css": ["css/style.css"],
"js": ["inject.js"],
"all_frames": true
}]

Resources