Linking to local files within css file - google-chrome-extension

I'm attempting to completely overhaul a websites css and possibly html and just trying to find a way that works for me.
The problem is that within an extension local css file I'm also trying to define a font url via extension local files.
manifest.json:
{
"name": "Test theme",
"description": "Custom theme for Darkmass.gg.",
"version": "0.0.1",
"manifest_version": 3,
"content_scripts": [
{
"matches": [
"https://*.example.com/*"
],
"css": [
"./css/main.css"
],
"js": [
"./js/app.js"
],
"run_at": "document_end"
}
],
"web_accessible_resources": [
{
"resources": [ "./media/*" ],
"matches": [ "https://*.example.com/*" ]
}
]
}
main.css:
#font-face {
font-family: NFLDolph;
src: url('chrome-extension://ipbedjgbhlnngdddbaojpnaicdpifmgd//media/fonts/NFLDOLPH.TTF');
}
The problem clearly lies with the source url for the NFLDolph font-face, if I just leave it as ./media/fonts/NFLDOLPH.ttf than it just tries to load the font from example.com instead of locally.
And here is the error I get:
Denying load of chrome-extension://ipbedjgbhlnngdddbaojpnaicdpifmgd//media/fonts/NFLDOLPH.TTF. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

I made a sample.
manifest.json
{
"name": "NFLDolph",
"version": "1.0",
"manifest_version": 3,
"content_scripts": [
{
"css": [
"main.css"
],
"matches": [
"<all_urls>"
]
}
],
"web_accessible_resources": [
{
"resources": [
"font/*.TTF"
],
"matches": [
"<all_urls>"
]
}
]
}
main.css
#font-face {
font-family: NFLDolph;
src: url("chrome-extension://__MSG_##extension_id__/font/NFLDOLPH.TTF");
}
* { font-family: NFLDolph, serif }

Related

Access open shadow-root from browser extension content script (Chrome / Firefox)

Within the elements tab of the browser development tools, the shadow root appears as #shadow-root (open) and it is accessible from the development console via document.getElementById('parent').shadowRoot, where the element with id of parent contains the shadow root. However, from the content script of a Chrome browser extension the same code results in null (while document.getElementById('parent') of course works as expected). Is it possible to access shadow-root from a browser extension?
TL;DR. Tried to access the shadow-root element from a browser extension in the same way that it is accessible from the developer's console. Expected it to work in a browser extension in the same way that it works in the developer's console.
manifest.json
{
"content_scripts": [ {
"exclude_globs": [ ],
"exclude_matches": [ ],
"include_globs": [
"https://*.test.com/*"
],
"js": [
"script.js",
],
"matches": ["http://*/*","https://*/*"],
"run_at": "document_idle",
//"run_at": "document_end",
"all_frames": true
} ],
"permissions": [
"cookies",
"storage",
"http://*/",
"https://*/",
"tabs",
"contextMenus",
"management",
"clipboardWrite",
],
"converted_from_user_script": false,
"description": "test",
"key": "--",
"name": "test",
"version": "1.0",
"manifest_version": 2
}
script.js
if (document.readyState == "complete" || document.readyState == "interactive") {
console.log(document.getElementById('parent').shadowRoot);
} else {
document.onreadystatechange = function () {
if (document.readyState == "complete" || document.readyState == "interactive")
{
console.log(document.getElementById('parent').shadowRoot);
}
}
}

Manifest v3, Failed to load extension: Invalid value for 'web_accessible_resources[0]'. Invalid match pattern. Could not load manifest

Given this Manifest v3 (after making it work on v2, I've adjusted some specs to match v3), I've some issue when adding the extension into Chrome (load unpacked).
The exact error is:
Invalid value for 'web_accessible_resources[0]'. Invalid match pattern.
Could not load manifest.
Here is the manigest
{
"author": "Hugo Gresse",
"description": "",
"name": "app",
"version": "1.0.0",
"content_scripts": [
{
"js": [
"src/entries/contentScript/primary/main.js"
],
"matches": [
"https://play.google.com/*"
]
}
],
"icons": {
"16": "icons/16.png",
},
"permissions": [],
"action": {
"default_icon": {
"16": "icons/16.png",
},
"default_popup": "src/entries/popup/index.html"
},
"host_permissions": [
"*://*/*"
],
"manifest_version": 3,
"web_accessible_resources": [
{
"resources": [
"assets/src/entries/contentScript/primary/main.5ebc631d.js",
],
"matches": [
"https://play.google.com/console/*"
],
"use_dynamic_url": true
}
]
}
According to the v3 specs specifics to the web_accessible_resources here, the pattern used in matches must only be tld based:
A list of URL match patterns specifying which pages can access the resources. Only the origin is used to match URLs. Origins include subdomain matching. Paths are ignored.
It says the "paths", here in my case: console/ is ignored, but it is not as it fail to load the extension.
✅: https://play.google.com/*
❌: https://play.google.com/console/*
The correct web_accessible_resources node is a follow:
"web_accessible_resources": [
{
"resources": [
"assets/src/entries/contentScript/primary/main.5ebc631d.js",
],
"matches": [
"https://play.google.com/*"
],
"use_dynamic_url": true
}
]

Manifest v3: net::ERR_FILE_NOT_FOUND when using chrome.runtime.getURL?

I get a net::ERR_FILE_NOT_FOUND error when using chrome.runtime.getURL function.
manifest.json
{
"name": "Dummy",
"manifest_version": 3,
"version": "1.0.0",
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"action": {
"default_icon": {
"16": "icon16.png",
"48": "icon48.png"
}
},
"permissions": ["activeTab", "storage", "scripting"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["content.js"]
}
],
"web_accessible_resources": [
{
"resources": ["src/images/*.jpg"],
"matches": ["<all_urls>"]
}
]
}
I'm using it in a constants.ts file which is not a service worker or content script, just a random file:
chrome.runtime.getURL('src/images/1.jpg')
But I don't get a URL but rather an error when I try to display it in a style tag:
style={{ background: `url(${chrome.runtime.getURL('src/images/1.jpg')})` }}
How do I solve it? I even tried opening the URL which looks like chrome-extension://hhnlljbelglibjololeadifpojkopfk/src/images/1.jpg in a new tab but it doesn't show anything.
How do I solve it?
When you use it in a CSS file or code line, try to use it like this:
style={{ background: url('chrome-extension://__MSG_##extension_id__/src/images/1.jpg') }}
For more information see this Chrome Developers page:
https://developer.chrome.com/docs/extensions/reference/i18n/#overview-predefined
Oopsie, as wOxxOm said, my copy-webpack-plugin wasn't copying images folder.
I changed my webpack.config.js to copy it.
plugins: [
new CopyPlugin({
patterns: [
{ from: 'public', to: '.' },
{ from: 'src/images', to: 'images/' },
],
}),
],

Injected script was not load

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?

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.

Resources