has no method getBackgroundPage - javascript

I am trying to learn about chrome extensions, and I think I am missing something. I want to upload a file using background script...here is my manifest:
"manifest_version": 2,
"name": "myTest",
"description": "Upload file",
"version": "0.1",
"icons": {
"64": "64.png",
"16": "16.png",
"32": "32.png",
"128": "128.png"
},
"background":{
"scripts": ["justupload.js"],
"persistent": true
},
"content_scripts": [
{
"matches": ["http://my.url/page.html"],
"js": ["link.js"]
}
],
"permissions": [
"http://my.url/page.html","background"
]
And link.js
function sendbg(){
var BGPage = chrome.extension.getBackgroundPage();
BGPage.senddata(document.getElementById('files'));
}
document.getElementById('files').addEventListener('change', sendbg, false);
When files changes, I receive the following error:
Uncaught TypeError: Object #<Object> has no method 'getBackgroundPage'
Thanks

Communication via content scripts needs to be done with message parsing.
check this and this

Related

Chrome extension error background.js:3 (anonymous function)

I don't know where came wrong. The chrome extension keep on showing error
manifest.json
{
"name": "CatExtension",
"description": "Coding train practice-communication between background script and content script",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "/images/cat.png"
}
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
]
}
background,js
chrome.action.onClicked.addListener(buttonClicked);
function buttonClicked(tab) {
console.log(tab);
}
It shows error
Where is the error?

Uncaught TypeError: Cannot read properties of undefined (reading 'network')

I'm currecntly working on a chrome extension and I need to access requests in network tab. Found the API called devtools.network. Tried to use it, but It threw an error like this:
content.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'network')
at content.js:1
content.js:
chrome.devtools.network.onRequestFinished.addListener(function(request){
console.log(request)
})
manifest.json:
{
"name": "Managebac Grade Calculator",
"version": "1.2",
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"https://opengate.managebac.com/student/*"
],
"js": [
"jquery.js",
"content.js"
]
}
],
"devtools_page": "devtools.html",
"permissions":[
"network"
],
"icons": {
"16": "pfp.jpg",
"48": "pfp.jpg",
"128": "pfp.jpg"
},
"browser_action": {
"default_icon": "pfp.jpg",
"default_popup": "popup.html"
}
}
Please help me

Display global page object from contentscript to popup in chrome extension

I have an object WIO on a webpage which i want to access in my chrome extension. I have followed this example and injected my script.js in the page using first method.
I can successfully read WIO object in script.js. But the problem is i can't access it in popup.html.
Any suggestions will be great appreciated.
Here is manifest.json
{
"manifest_version": 2,
"name": "Campaign Analyzer",
"description": "Analyze campaign data",
"version": "0.1",
"icons": { "128": "icon_128.png" },
"browser_action": {
"default_popup": "popup.html"
},
"permissions": ["activeTab", "<all_urls>"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["jquery-3.4.1.min.js", "inject.js"]
}
],
"web_accessible_resources": ["script.js"]
}
inject.js
s.src = chrome.runtime.getURL("script.js");
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
script.js
setTimeout(function() {
console.log(WIO.sessionInfo.context);
}, 6000);

Google Chrome content_script not loading JS on matching URL

Even after searching a lot of topics in stack overflow, nothing helped me to fix this error...
I'm trying to create an extension, and for now there are simple codes in it, but unfortunately, the console is not logging 'Hello, world!' from the content_scripts file.
manifest.json
{
"manifest_version": 2,
"name": "Example",
"shortname": "exmpl",
"description": "__MSG_appDesc__",
"version": "0.0.1",
"default_locale": "en",
"author": "Mateus Akino",
"icons": {
"16": "i16x.png",
"48": "i48x.png",
"128": "i128x.png"
},
"homepage_url": "http://example.com/",
"browser_action": {
"default_icon": "i32x.png",
"default_popup": "popup.html"
},
"update_url": "http://example.com/update.xml",
"chrome_url_overrides": {
"newtab": "newtab.html"
},
"content_scripts": [{
"matches": ["*://*.youtube.com/*"],
"js": ["execute.js"],
"run_at": "document_end"
}],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"activeTab", "tabs", "i18n", "management", "webNavigation", "<all_urls>"
]
}
execute.js
console.log("Hello, world!");
background.js
chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) {
chrome.tabs.executeScript(null, {
file: "execute.js"
});
});
I fixed the problem, so I'm posting it here if someone else has the same issue.
Looks like the code was fine, the problem was the way I was loading the extension...
As I'm using 'Load unpacked extension', my manifest.json wasn't updating just by disabling and enabling it (neither by using Refresh extensions now).
So I removed the extension, loaded it again and it's working normally now.

How to get data from chrome extension local storage?

I'm developing a chrome extension, popup.html saves some data into extension's local storage (key:viewMode value:WYSIWYG)
When I'm trying to retrieve the data using Chrome Storage API from script.js, it says that viewMode is undefined.
Here is manifest.json
{
"manifest_version": 2,
"name": "LoremIpsum",
"description": "Dolor sit amet",
"version": "1.2.8",
"permissions": [
"http://*.exemple.org/*",
"storage"
],
"browser_action": {
"default_icon": {
"19": "icons/19.png"
},
"default_title": "LoremIpsum",
"default_popup": "popup.html"
"icons": {
"16": "icons/16.png",
"19": "icons/19.png",
"48": "icons/48.png",
"128": "icons/128.png"
},
"web_accessible_resources": [
"img/*.png",
"smilies/*.gif",
"style.css"
],
"content_scripts": [
{
"matches": ["http://*.exemple.org/*showtopic*", "http://*.exemple.org/*act=ST*"],
"css": ["style.css"],
"js": ["jquery-1.10.2.min.js", "popup.js", "script.js"]
}
]
}
I just realized that I made a dumb error while setting chrome.storage.local. I've passed arguments as an array instead of passing them as an object.

Categories

Resources