I'm creating a Chrome extension for the first time.
I've used chrome.notifications.create in it, but it does not work! This is my code:
chrome.storage.sync.get('refresh_time',function(items){
status = $('td.c').next().html();
if (status.trim() == 'SomeText') {
alert('Works');
var opt = {
type: "basic",
title: "Project1",
message: "This is my first extension.",
iconUrl: "icons/icon-128.png"
};
chrome.notifications.create('statusChanged', opt, function(){});
}
})
I get the alarm after execution, but chrome notification does not work! May you tell me what is wrong in my code?
By the way, I used the code below in my manifest file.
"permissions" : [
"storage",
"notifications",
"tabs"
]
I think you need to make iconUrl a chrome extension path, something like this:
iconURL: chrome.runtime.getURL("icons/icon-128.png")
I think that the code is good. However, I guess that you need to confirm the iconUrl path. If the file specified by the path does not exist, the notification will not be displayed. At the time, you may see the following error message on the console tab of the DevTools:
Unchecked runtime.lastError while running notifications.create: Unable to download all specified images.
You need to specify the valid path for the iconUrl value.
For me, the notifications were disabled on windows. Make sure that "Get notifications from apps and other senders" is enabled.
Probably, you need to modify the image path as:
iconUrl: "../icons/icon-128.png"
Related
I'm migrating V2 to V3, but encountered some strange problems.
When extension has loaded, i fetching the resource, it will has a error: net::ERR_BLOCKED_BY_CLIENT. but it's no problem when i do it without loaded extension.
like this:
without loaded extension
with loaded extension
Extension launch up with a error:
GET chrome-extension://hbdmkhahfinoeomfcllpnpkcblpaofeh/css/style.css net::ERR_BLOCKED_BY_CLIENT
'web_accessible_resources' in content.js
var css1 = doc.createElement("link");
css1.href = chrome.runtime.getURL("css/style.css");
css1.type = "text/css";
css1.rel = "stylesheet";
doc.head.appendChild(css1);
code in manifest.json
"web_accessible_resources" : [{
"resources": [ "css/*", "content/*" ],
"matches": ["<all_urls>"]
}],
Converting the txt file to html is the main responsibility of this extension.
I have disabled Adblock Plus, and try to fix this problem pass a whole day, I really don't know what happened in this condition and how to fix it.
In background.js, I create a popup like so:
chrome.windows.create({
focused: true,
width: 1170,
url : "settings/index.html",
type: "popup"
}, function(popup) {
tab_app = popup.id;
alert(tab_app);
});
I store the id in tab_app.
how can I pass a value from background.js to my popup?
I'm trying like that:
chrome.tabs.executeScript(tab_app, {code: "alert("+message.add+");"});
but it keeps telling me that this tab id doesnt exist.. im assuming its because its a popup. will appreciate some help.
Since it's your extension page, the method of choice is Messaging.
Note: you can't use the per-tab messaging of chrome.tabs.sendMessage, since this explicitly targets the content script context (that doesn't exist for extension pages). You need to use the "broadcast" chrome.runtime.sendMessage that will send to all other extension pages.
If you can have more than one popup-type window at a time, this may be a problem - you need some identifier to go along. You could pass it as a URL parameter or a URL hash, e.g. "settings/index.html?id=foo" or "settings/index.html#foo". If you don't expect more than one popup-type window (you can always check if one is open before opening a new one), it doesn't matter.
If you really need dynamic code loading or execution, not just passing data (doubtful), you need to be mindful of CSP.
You can dynamically load a script from your extension's package by just creating and adding a <script> tag to the document.
However, you can't, by default, pass a string of code and eval it in the extension context. You could add 'unsafe-eval' to CSP string, but that's a bad idea in general.
Most probably, you only need some commands to be passed along with data. Pure messaging is great for it, just look at the docs.
This old answer of mine may be of use - I'm using opening a new tab and passing data there to print it.
You cannot call executeScript in the your extension pages. If you try to use executeScript in your extension page. It will show error :
Unchecked runtime.lastError while running tabs.executeScript: Cannot
access contents of url
"chrome-extension://extension_id/yourPage.html".
Extension manifest must request permission to access this host
Now you cannot add "chrome-extension://<extension_id>/yourPage.html" under permissions in manifest.json because it is invalid and not allowed.
Instead you can use message passing.
background.js:
function createNewtab(){
var targetId = null;
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
if (tabId != targetId || changedProps.status != "complete")
return;
chrome.tabs.onUpdated.removeListener(listener);
chrome.tabs.sendMessage(targetId, {message : "loadNewTab"},function(response){
// do nothing yet
});
});
chrome.windows.create({
focused: true,
width: 1170,
url : chrome.extension.getURL("settings/index.html"),
type: "popup"
}, function(popup) {
targetId = popup.tabs[0].id;
});
}
index.js:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
switch (request.message){
case "loadNewTab":
alert("HI")
break;
}
});
Possible duplicate question
Prior to asking this, I did some searching and found this question that is similar by name, but I don't understand if it's what I need:
Chrome Extension: how to capture selected text and send to a web service
Especially Mohamed Mansour's answer looked promising, but after spending over an hour researching, experimenting and testing, I still don't understand it. So I hope this possibly duplicate question will at least help other people with the same confusion that I have.
Question
Besides the title of this question, I'll include some details of what I want to do:
Select text on any website
Right-click to open the context menu of my chrome extension
Send that text to my website's database.
But for testing purposes, I thought I would replace the third point with sending a basic post-request to a site like hurl.it
So how do I send such a post-request from my chrome extension to a specific external website?
I already know how to get the selected text:
manifest.json
{
"manifest_version": 2,
"name": "My Plugin",
"description": "Test extension",
"version": "0.1.20150917",
"permissions": [
"contextMenus"
],
"background": {
"scripts": ["script.js"]
}
}
script.js
function handleSelectedText(info,tab) {
var selectedText = info.selectionText;
console.log("Selected text: " + selectedText;
//This where I thought I'd send the data to my domain
//And some pseudo-code to show how I thought it would work:
/*
chrome.extension.postRequest(selectedText, function(response) {
if(response == success)
displayNotification("Yay, it worked!");
else
displayNotification("Error: " + response.errorMessage);
});
*/
}
chrome.contextMenus.create({
title: "Mark error: '%s'",
contexts:["selection"],
id: "cc-mark",
onclick: handleSelectedText,
});
If I insert Mohamed Mansour's code into mine, I just get an error saying that response is undefined, so I'm assuming I'm doing that so terribly wrong that I shouldn't even include it.
If it isn't obvious already, I should mention that I'm new to Chrome extensions.
I'm trying to fire a chrome notification ( in a chrome extension ) with the following code:
var opt = {
type: "basic",
title: "Deploy",
message: "It worked!",
iconUrl: "test.png"
};
chrome.notifications.create("", opt, function(id) {
//console.error(chrome.runtime.lastError);
});
But it shows me the following message :
"Unchecked runtime.lastError while running notifications.create: Unable to successfully use the provided image. "
My image "test.png" is in the root folder of my app( with the manifest.json file ), but I've tried to put the picture also in other folder of my app, it never worked!
I've correctly added the permission for notifications, so I really don't know where the problem is !
Any ideas ?
Thank you,
EDIT: I found the solution ! The image needed to be 80*80 !
I'm making a Chrome Extension.
I want to use a image file on other servers as an icon of webkitnotifications(Desktop notifications)
in Content Script
window.webkitNotifications.createNotification(
"http://www.example.com/icon48.png", "title", "text"
).show();
in manifest.json
"permissions": [
"tabs", "notifications", "http://*/*", "https://*/*"
]
but I got the following error
Uncaught Error: SECURITY_ERR: DOM Exception 18
The URL of the icon changes in many ways. So I cannot add all of them to "web_accessible_resources" in manifest.json beforehand.
So, how can I do the above ??
Thank you very much in advance.
In my extension, with Chrome 23, I can point to external icon url like this without any problem
var notification = webkitNotifications.createNotification(
'http://placehold.it/48x48', // no need to add to "web_accessible_resources" beforehand
title, // notification title
textToDisplay // notification body text
);