chrome extension background script confirm message title - google-chrome-extension

I have a chrome extension. In its background script I want to present a confirm() dialog box to accept (Ok/Cancel) input from user. Have no problem doing that.
The only problem is that the title of message shows up as "chrome-extension://32_char_extension_id". Not what I want to see.
On the other hand in the safari extension (global.html) the title shows up as the "Extension name".
Would be nice to have the same functionality in Chrome Extension.
Any idea if there is a way to see extension name in the message box instead of the ID.
Anything I"m doing it wrong ?
I understand confirm() is the not the recommended solution for prompt to take user input (as it is a blocking call). But this seems like a quicker solution for what I"m doing.
The other alternatives I"m looking into using JQuery message boxes. But if I can resolve this thru simple confirm(), it would be great for now.

Probably, you should use the chrome.notifications API to display the dialog so that you ask the user something in this case. You can show a notification which has two buttons by like the following code:
chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) => {
// If buttonIndex == 0, Ok button clicked.
chrome.notifications.clear(notificationId, wasCleared => { ... });
});
chrome.notifications.create("notificationId", {
type: "basic",
iconUrl: "SOME_ICON_PATH",
title: "YOUR_EXTENSION_NAME",
message: "SOME_MESSAGE",
buttons: [
{
title: "Ok"
},
{
title: "Cancel"
}
]
});
Of course, you need to get a "notifications" permission from the user.

You can't change window.confirm() title, it is for anti-phishing.
You should try other approaches such as Bootstrap Modal

Related

How find to whether my Chrome extension was called by shortcut?

I have a Chrome extension with a popup that can also be opened by a shortcut. When the popup gets opened, can I find out whether the user has used the shortcut or whether they have clicked on the extension icon?
The reason is that I'd like to hint users to use the shortcut, but I don't want to show that hint to users who already know and use the shortcut.
Popup and shortcut are defined like this in manifest.json:
"browser_action": {
"default_icon": "images/icon48.png",
"default_popup": "popup.html",
"default_title": "__MSG_tooltip__"
},
"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+Space"
}
}
},
chrome.browserAction.onClicked.addListener
Not available, because:
Fired when a browser action icon is clicked. This event will not fire
if the browser action has a popup.
We has popup.
chrome.commands.onCommand.addListener
Not available, because:
The '_execute_browser_action' and '_execute_page_action' commands are
reserved for the action of opening your extension's popups. They won't
normally generate events that you can handle.
May try inject press listener to some page and track pressed of keys (on each page).
var isPressed;
document.body.addEventListener("keydown", function (e) {
if (!(e.keyCode != 17) || !(e.keyCode != 16) || !(e.keyCode != 32)) return;
isPressed = true;
});
From popup in moment expand send message to content_scripts:
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "isPressed"}, function (responce) {
if (responce) {
}
});
});
After message receiving, listener in content scripts send variable isPressed as response:
chrome.runtime.onMessage.addListener(function (message, sender, response) {
if (message.action == "isPressed") {
response(isPressed);
}
});
If variable is true, means called via keystrokes, else on click on icon.
can I find out whether the user has used the shortcut or whether they
have clicked on the extension icon?
There seems to be no clean direct way of detecting that.
Since I put some effort into trying to find a workaround, I would like to share what I have considered/tried and reasons why it doesn't work:
Attach the keyboard shortcut to some custom command, modify popup page and then open it programmatically.
Doesn't work because there is no way to open the popup programmatically, at least not in current stable version of Chrome (v50).
Create a custom command with the same shortcut as "_execute_browser_action" and use it to send a message to the popup.
Doesn't work because two commands cannot share the same shortcut.
Try to capture keyup in the popup page immediately after loading.
I tested this and it seemed to work at first, but it is definitely not reliable. If the user only presses the keyboard shortcut very briefly, the keyup event is fired before the popup page gets a chance to register a listener for it.
Capture keydown on pages using a content script and then send a message to the popup page to let it know that the keyboard shortcut was pressed (as suggested by UserName above).
This should work on most pages, but the content script won't be able to capture the keypress in address bar and on certain pages (chrome://, chrome-extension://).
Difficult to implement because you need to take into account the fact that users can customize the shortcut on chrome://extensions page. Finding the currently assigned shortcut programmatically to test against it in the content script is surprisingly difficult, because chrome.commands.getAll() provides localized key names (eg. "Ctrl+Shift+Space" in English, but "Ctrl+Shift+Mezera" in Czech) There are languages where even Ctrl and Shift don't stay in English.

Notification action button is not shown in the notification box

can anyone please give the syntax for chrome notification containing buttons??I am not sure about the syntax for adding buttons to notification box in chrome.Can anyone please help me??
This is my code for notification.
chrome.notifications.create(
'id1',{
type: 'basic',
iconUrl: '/icon_128.png',
title: 'Althe Frazon',
message: 'Lorem ipsum',
buttons: [{ title: 'Call'
},
{ title: 'Send Email'
}],
priority: 0
},
function() { /* Error checking goes here */}
);
notification.show();
Using this code the notification is shown.But the buttons are not shown.Anygone please guess the reason.Why my buttons are not shown??Please help me
For anyone landing on this page looking for a way to add buttons to notifications or trying to figure out why no buttons are shown:
This answer explains in detail how to create notifications and how to add buttons.
Nonetheless, the buttons are not displayed on some distributions of Linux.
This is a known issue (still holding for Chrome version 31.0.1650.57) and is dependent on Views shipping on Linux.

Cool way to reset the browser action badge?

I have an extension that implements a browser action.
Of course, the browser action is allways visible, but it has a special significance in certain urls. So, I use filtered events to listen to those urls and set the proper badge
chrome.webNavigation.onDOMContentLoaded.addListener(
function(tab){
chrome.browserAction.setBadgeText({
text:'bdge',
tabId: tab
});
},
{'url':[{hostSuffix: 'somedomain.com', pathPrefix: 'somePath/'}]}
);
Is there some "elegant" way to reset the badge when the user navigates out from that page, without listening every single tab navigation?
Should I execute a content script to hang on some exiting event and send a message?
Thank you very much,
It seems to me that a good solution would be to use chrome.tabs.onUpdated.
In your background page, you would have something like that:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// using a regex or however else you want to test the URL
if (/somedomain\.com\/somePath\//.test(changeInfo.url)) {
chrome.browserAction.setBadgeText({
text: 'bdge',
tabId: tabId
});
} else {
chrome.browserAction.setBadgeText({
text: '',
tabId: tabId
});
}
});
I know you wrote "without listening every single tab navigation" but I'm not sure why you want to avoid this.
This is what documentation doesn't tell you: Chrome actually resets badge automatically when user navigates away.
When you set a browser action's badge only to a specific tab, like
chrome.browserAction.setBadgeText({
text: 'ABCD', // My badge's text should be only 4 characters long
tabId: 1234 // Any tab, ussually a var here, not a constant
});
Chrome shows the badge on the browser action button only when that tab is the active tab in the window. Its text resets to '' when the user navigates away in that tab. No need for special action to reset it.

Crossrider setPopup not callling more then once

I have a problem calling crossrider setPopup multiple times based on
some condition like if user is logged in then only show the Popup on
browser action click otherwise show a login popup.
But, it is calling only once after that.
function handler(evtXHR) {
if (invocation.ready State == 4) {
if (invocation.status == 200) {
//alert("successs : "+invocation.responseText);
if (invocation.responseText == "demo") {
//buttonState = true;`enter code here`
appAPI.browserAction.setPopup({
resourcePath: 'html/New popup.html',
height: 1000,
width: 1000
});
}
In general, calling setPopup more than once changes the content for the subsequent button click. However, having reviewed your code, I can see that there are several issues with the extension, for example, for correct operation the button must be enabled in the Settings > Browser Buttons (see introduction to browserAction and How to add a button to your extension).
I don't think StackOverflow is the appropriate forum to assist you in debugging the extension issues and therefore, invite you to email our support channel (support#crossrider.com) with the details you provided and I will be happy to assist you further

Communicating with a content script on active tab from a Chrome extension's page action popup

I'm not getting how to pass data between content script and page action popup.
I've started with the following simple skeleton, that shows an page action for any page having a minus-dash in title:
Extension manifest (manifest.json):
{
…
"permissions": ["http://example.org/*"],
"background": {"scripts": ["background.js"]},
"page_action": {"default_popup": "popup.html", …},
"content_scripts": {
"matches": ["http://example.org/*"],
"js": ["content.js"]
}
}
Background script (background.js):
chrome.extension.onRequest.addListener(function (msg, sender, respond) {
if (msg && msg.what === "match") {
console.log("Match:", msg.title);
chrome.pageAction.show(sender.tab.id);
}
}
Content script (content.js), checking document titles:
var title = document.title;
if (title.indexOf("-") >= 0) {
chrome.extension.sendRequest({"what": "match", "title": title});
}
Now, in a page action's popup, I want to show matched page's title. Not the last loaded page's title (as would be done by using this answer), but the title of the active tab.
My first though was to send a query to the content script, but according to documentation chrome.extension.sendMessage will send it to all listeners (i.e. all my content scripts on all tabs), without any clear definition on whose response I'll receive back. Also, I can't use chrome.tabs.sendMessage as it requires tabId. Trying to find the current tab using chrome.tabs.getCurrent will return null, as the query comes from non-tab context (from a popup).
I guess I could probably use chrome.tabs.executeScript to communicate, but this just feels dirty.
Still, I believe, this is a basic thing that should be very simple to do and I'm just missing something. Could someone, please, help me, preferably, with an example code?
Update: I've found Mappy example extension and it uses chrome.tabs.onUpdated to keep track of the active tab. This, unfortunately, requires tabs permission. Personally, I'd like to use least privileges possible.
So, is it just unfortunately bad permission system design and I have no choice but to do it this way, or there's any workaround? I'd be happy if chrome.pageAction.onClicked event handler (which provides Tab object that I need) would allow me to show a popup...
I think you need to add the onClick event listener in your popup:
chrome.pageAction.onClicked.addListener(function(tabs.Tab tab) {...});
See documentation here.
Callback of the event listener would provide you the tabId which would surely be the active tab.
There are multiple Problems in your code
chrome.extension.sendRequest in chrome.extension.sendRequest({"what": "match", "title": title}); is deprecated
chrome.pageAction.onClicked will not fire when you have "page_action": {"default_popup": "popup.html", …}, in your manifest.
chrome.extension.sendMessage will send it to all listeners (i.e. all my content scripts on all tabs), is an invalid assumption, it will send to Extension Pages.
I tried to read your question multiple times but couldn't understand what is you want to achieve, could you explain it?

Resources