Chrome Extension Using Manifest 3 and Multiple Scripts - javascript

I am trying to import scripts and am apparently only able to get one. I am using this manifest:
{
"manifest_version": 3,
"name": "Auto_Select",
"description": "This extension auto selects Mturk HITs",
"version": "1.0",
"action": {
"default_icon": "auto_select.png",
"type": "module",
"default_popup": "auto_select.html"
},
"background": {
"service_worker": "worker_wrapper.js"
},
"permissions": [
"tabs",
"activeTab"
],
"host_permissions": [
"<all_urls>"
]
}
This is "worker_wrapper.js":
try {
importScripts("js.cookie.js", "auto_select.js", "cookie_mgmt.js");
} catch(e) {
console.log(e);
}
When I try to load the extension I get this error:
ReferenceError: document is not defined
at auto_select.js:26:1
at worker_wrapper.js:4:5
All the scripts are in the root directory for the extension so I don't understand why it doesn't error on the first script but rather on the 2nd. Can someone help me with this? TIA.

Related

Chrome extenstion - error: Cannot access chrome:// URL (Manifest 3)

Chrome Extension - Manifest V3
I am trying to build a simple chrome extension and am getting stuck on a particular error.
Uncaught (in promise) Error: Cannot access a "chrome://" URL
I found a way to counter the error, but it does not solve the problem itself.
I understood that when I use my extension, it works on "chrome://..." which I see when I inspect the extension, but can not figure out how to change that (if that even needs changing)
My code:
manifest.json
{
"version": "0.1.0",
"manifest_version": 3,
"name": "Super cool name",
"description" : "A super cool intro",
"author": "you wouldn't know him",
"permissions": ["storage", "activeTab", "scripting", "tabs"],
"host_permissions": ["tabs", "notifications", "http://*/*", "https://*/*"],
"background": {
"service_worker": "background.mjs",
"type": "module"
},
"action": {
"default_icon": {...},
"default_popup": "popup.html"
},
"icons": {...},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["contentScripts.mjs"]
}]
}
background.mjs
chrome.tabs.onUpdated.addListener((tabId, tab) => {
if (tab.url?.startsWith("chrome://")) return undefined;
if (tab.active && changeInfo.status === "complete") {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: ['contentScripts.mjs']
Any tips? Thank you for your time!

How to execute content script using chrome extension to a url starting with "chrome-extension://....../options.html"

I have used "<all_urls>" for permissions.
But, I am getting this error. "Cannot access contents of the page. Extension manifest must request permission to access the respective host."
chrome.tabs.create({ url: 'chrome-extension://padekgcemlokbadohgkifijomclgjgif/options.html#!/profile/proxy' }, function(tab) {
chrome.tabs.executeScript(tab.id, { file: 'js/content.js' });
});
manifest.json
{
"manifest_version": 2,
"version": "1.0.0",
"name": "Gmail Automation",
"description": "Automation",
"permissions": [
"storage",
"notifications",
"<all_urls>"
],
"icons": {
"16": "resources/img/16.png",
"32": "resources/img/32.png",
"48": "resources/img/48.png",
"64": "resources/img/64.png"
},
"web_accessible_resources": ["js/content.js"],
"background": {
"persistent": false,
"scripts": [
"/js/background.js"
]
}
}
If this is not possible. Then please suggest me other ways to do. My goal is to change or modify options in other existing extensions.

Cannot read property 'onBeforeNavigate' of undefined (Chrome Extension)

I simpley want to get the extension fired if the action on 'onBeforeNavigate' happens. But for no reason the extension always trows this error:
Cannot read property 'onBeforeNavigate' of undefined
Error is on this line chrome.webNavigation.onBeforeNavigate.addListener(setProxy);
Here is my code so far:
popup.js
function setProxy(details){
console.log("Got fired!\n");
}
chrome.webNavigation.onBeforeNavigate.addListener(setProxy);
manifest.json
{
"manifest_version": 2,
"name": "Proxy Changer",
"description": "This extension Changes the Proxy settings every time you load a website",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"activeTab",
"webRequest",
"notifications",
"debugger",
"background",
"management",
"https://ajax.googleapis.com/",
"<all_urls>",
"webRequestBlocking"
],
"background": {
"scripts": ["popup.js"],
"persistent": true,
"js": ["jQuery.js", "popup.js"]
}
}
I hate to say that I am very new to the Google Chrome Extension API but for me the Extension API is not working... I don't know what I am doing wrong..
I already watched to videos but still same error...
I would appreciate your help. Thank you and kind regards!
just add "webNavigation" permission to Manifest.json
{
"name": "My extension",
...
"permissions": [
"webNavigation"
],
...
}
see documentation
https://developer.chrome.com/docs/extensions/reference/webNavigation/#event-onCreatedNavigationTarget

Trying to make an extension

I have an extension that i'm trying to add content scrips alongside background scripts but it just says invalid when trying to temp load.
{
"description": "Creates tasks and calculates application incomplete date",
"manifest_version": 2,
"name": "Task Creator",
"version": "1.31",
"permissions": [
"http://*/*", "tabs", "https://*/*",
],
"icons": {
"48": "icons/page-48.png"
},
"web_accessible_resources": [
"style/popUpStyle.css",
"script/popUpTask.js",
"script/logicTaskFiller.js",
"js/autosize.js",
"style/jquery-ui.css",
"js/jquery-1.12.4.js",
"js/jquery-ui.js"
],
"content_scripts":{
"matches": ["*urlhere.com*"],
"js": ["comSendForm.js"]
},
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/page-32.png"
}
}
I'm not quite sure where i'm messing up. It works instantly after I take out the content scripts, but I'm doing multiple things with this extension and I really do need the content scripts to run on a certain page. Any help is appreciated.
error message
1477430058898 addons.webextension. ERROR Loading extension 'null': Reading manifest: Error processing content_scripts: Expected array instead of {"matches":["*://url.com/"],"js":["comSendForm.js"]}
The error that you are getting is that your manifest.json has the value of the content_scripts key as an Object. The value of the content_scripts key needs to be an Array of Objects, not just an Object.
In addition, you have the following problems:
The line:
"http://*/*", "tabs", "https://*/*",
should not have the trailing ,. This is actually reported as the first error, so you may have copied the contents of your manifest.json file inaccurately.
Your matches pattern is invalid. You probably wanted something like:
"matches": ["*://*.urlhere.com/"],
With all of those changes your manifest.json would look like:
{
"description": "Creates tasks and calculates application incomplete date",
"manifest_version": 2,
"name": "Task Creator",
"version": "1.31",
"permissions": [
"http://*/*", "tabs", "https://*/*"
],
"icons": {
"48": "icons/page-48.png"
},
"web_accessible_resources": [
"style/popUpStyle.css",
"script/popUpTask.js",
"script/logicTaskFiller.js",
"js/autosize.js",
"style/jquery-ui.css",
"js/jquery-1.12.4.js",
"js/jquery-ui.js"
],
"content_scripts": [
{
"matches": ["*://*.urlhere.com/"],
"js": ["comSendForm.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/page-32.png"
}
}

I don't understand Google Chrome extensions. Please help.

I want to do something that seems like it should be fairly simple, but I can't get it to work.
All I want it to do is when the button in the popup is clicked I want to log the word "Hello" to the console. At the moment nothing happens. No error message. Just nothing.
Here is my manifest.jason file
{
"name": "Content Script",
"version": "1.0",
"description": "Experiments with content scripts.",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
}
}
Here is my popup.html
<h1>Hello</h1>
<script>
function changeField() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {"code": "sayHello"});
});
}
</script>
<button onclick="changeField();">Click</button>
Here is my contentscript.js
function sayHello() {
console.log("Hello");
}
chrome.extension.onRequest.addListener(
function(request, sender, response) {
if(request.code == 'sayHello') {
sayHello();
}
}
);
I've been reading the docs but they seem to skip over a lot of things. If someone could explain why this doesn't work I would be eternally grateful.
You didn't inject your content script to the page which console lives in.
Add content_scripts segment to your manifest.json file.
There is a reference of manifest.json file on chrome extension official site.
{
"name": "Content Script",
"version": "1.0",
"description": "Experiments with content scripts.",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"popup": "popup.html"
},
"content_scripts":[{
"matches":["http://*/*"],
"js":["content_script.js"]
}]
}

Categories