This question already has answers here:
TypeError: [API] is undefined in content script or Why can't I do this in a content script?
(2 answers)
Closed 5 years ago.
I am using the Developer Version of Firefox (Version 54.0a2, but also tried the normal Firefox with Version 51) and I want to include notifications in my web extension.
browser["notifications"] does not exist though.
Since it was not working in my extension and I thought maybe something was conflicting I created this very basic web-extension.
the manifest.json:
{
"manifest_version": 2,
"name": "Extension",
"version": "1.0",
"description": "...",
"icons": {
"48": "icons/icon-48.png"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["test.js"]
}
],
"permissions": ["notifications", "storage"]
}
test.js (storage works just fine btw.)
if (browser["notifications"]) {
console.log('Notifications exist!');
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/icon-48.png"),
"title": "test",
"message": "test"
});
}
else {
//it always executes this part :/
console.log('notifications do not exist');
console.log(browser);
}
Debugging the add-on doesn´t show any errors either.
Many of the apis exclusive for extensions can only be run inside background script. The usual technique if you need to run it from a content script is to send a message from content script to background script, handle the message in background script and run from there the command you wanted.
In your case, there are a few examples at the end of Notification Api, one of them being notify-link-clicks-i18n where you can view how they do the message passing to create the notification from the background script.
Related
I am trying to make a chrome extension that alerts you in the tab that you are currently moving or highlighting. I have tried reading the chrome migrating to V.3 documentation and have come up with the following code, however, the alerts never appear. Does anybody know what I need to change or add?
// manifest.json
{
"manifest_version": 3,
"name": "Alert",
"version": "0.1",
"description": "alerts you when doing tab functions",
"permissions": ["tabs", "activeTab"],
"host_permissions": ["<all_urls>"],
"background": {
"service_worker": "background.js"
}
}
//background.js
chrome.tabs.onMoved.addListener(function () {
alert("You moved this tab");
});
chrome.tabs.onHighlighted.addListener(function () {
alert("You highlighted this tab");
});
Working directory:
.
├── background.js
├── manifest.json
alert() method cannot be used outside of the browser environment, you can use console.warn() or console.error() instead. But is not a good solution if you want to show an error message to the extension user, as they would never open the console.
If you would like a more user friendly approach use the following:
chrome.notifications.create({
type: 'basic',
iconUrl: '/images/image_if_any.png',
title: `Notification title`,
message: "Your message",
priority: 1
});
Also add "notifications" to "permissions" in your manifest.json file:
"permissions": [..., "notifications"]
alert is not defined in a service worker per specification so we'll have to use console.log
Also, I was looking in the wrong place for the alert messages. I needed to look at the service worker link in my unpacked extension page.
Ok so I am trying to get a chrome extension to block mature websites but for some reason it is giving me an error. Here is the error: "Unchecked runtime.lastError: '://.youtube.com' is not a valid URL pattern." and here is the code:
{
"name": "Video Testing",
"version": "1.0",
"description": "Video testing extention",
"permissions": ["webRequest", "webRequestBlocking","<all_urls>"],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
here is background.js
const defaultFilters = [
"*://*.youtube.com"
]
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return { cancel: true}},
{ urls: defaultFilters },
["blocking"]
)
So i hope someone could help me. Thanks!
You forgot a forward slash at the end of the URL. The provided code in the question would not work for me until I added it, and the error appears to come back without it.
const defaultFilters = ["*://*.youtube.com/"]
As stated above, you'll want to use the below format if you wish to block all associated youtube URLs (such as video or channel links) as opposed to just youtube.com
const defaultFilters = ["*://*.youtube.com/*"]
I want to make a chrome extension which parses youtube/soundcloud/... pages and gets current song's name. Then it should update user's rich presence status in discord. Like a did it in python there.
I have that one so far. Title already parsed, there's no problem. I have a problem with that code works. Because it doesn't work.
const clientId = '605777168739598338';
const scopes = ['rpc', 'rpc.api'];
const client = new RPC.Client({ transport: 'websocket' });
client.connect();
function updatePresence(title, time, icon) {
title = title.replace(/["]/g, "\\\"");
client.setActivity({
details: title,
startTimestamp: time,
largeImageKey: icon
}, 9999)
}
I also tried raw websocket connection but I'm stupid..
UPD:
The code above is in content.js.
browser.js is a file copied from root of module discord-rpc which i downloaded via npm.
manifest.json
{
"manifest_version": 2,
"name": "Tomori Player",
"version": "0.1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["browser.js"]
},
"permissions": [
"ws://localhost:6463/*",
"tabs",
"webRequest",
"webRequestBlocking"
],
"content_scripts": [
{
"matches": [
"https://www.youtube.com/watch*",
"https://youtube.com/watch*"
],
"js": ["content.js"]
}
]
}
P.S. I'm so sorry. I'm new in JS.
There's a solution I found:
Chrome Extension parses page and sends sockets to another app
Another app on your PC gets these sockets and then sends RPC to Discord
Timeraa#7947 (PreMiD dev): Discord will disconnect almost immediately if you connect with a browser, trust me i tried. You will need to have an application running in the background
So, you can use PreMiD and push up PreMiD's presences list or make your own app to do it.
This question already has an answer here:
How to access bookmarks page and other chrome urls in an extension?
(1 answer)
Closed 8 years ago.
I'm working on a Google Chrome Extension called SearchTab (https://chrome.google.com/webstore/detail/searchtab/ehakebjpbbcppbapabbkomdinmnjppjh)
I'm trying to add a link to chrome://apps, but its not allowing the redirect. I checked the console and I got this error: Not allowed to load local resource: chrome://apps/
Here is my html:
<img class="apps" src="images/chrome-apps.png"/>
Here is my javascript:
$('.apps').click(function () {
window.location.replace("chrome://apps/");
});
I think there must be some permission I need to add to my manifest, but I don't know which one.
My manifest:
{
"background": {
"persistent": false,
"scripts": [ "script/background.js" ]
},
"chrome_url_overrides": {
"newtab": "dashboard.html"
},
"description": "Upgrade Your New Tab Page To SearchTab",
"icons": {
"16": "images/icons/icon16.jpg",
"48": "images/icons/icon48.jpg",
"128": "images/icons/icon128.jpg"
},
"manifest_version": 2,
"name": "searchTab",
"offline_enabled": true,
"permissions": ["unlimitedStorage", "tabs"],
"version": "0.0.7"
}
Since that you're not allowed allowed to load local resources with external JavaScript scripts, you'll have to use the chrome.tabs API. You can .create() a new tab or .update() an existing one.
chrome.tabs.create({url: "chrome://apps/", active: true});
// or:
chrome.tabs.update(/*tabId*/, {url: "chrome://apps/"});
I am building a Chrome Extension and I am having trouble adding a JavaScript library to use in my content script.
I am trying to add the Mutation Summary Library. I put the 'mutation_summary.js' file into the extension's directory and I tried to add it by adding 'mutation_summary.js' to the 'manifest.json' file as shown below:
"content_scripts": [
{
"matches": ["http://soundcloud.com/*", "https://soundcloud.com/*"],
"js": ["content_script.js", "mutation_summary.js"]
}
An extension error is thrown when I add it. The errors says "Could not load javascript 'mutation_summary.js' for content script.
Am I adding the javascript library incorrectly?
Thanks
It works perfectly for me. I created an extension with a file called mutation_summary.js which contains console.log('FILE LOADED!'); and it shows up as soon as I load soundcloud.com, maybe you had a typo with the file name?
manifest.json:
{
"name": "Extension",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": ["http://soundcloud.com/*", "https://soundcloud.com/*"],
"js":["mutation_summary.js"]
}
]
}