First of all, sorry for my english, but I try my best) I have no idea whats wrong with my chrome extension. In background script I create some notifications with action by click on it. Action - open new tab with some link. When I click on the notification at first time - everything is ok, but if I click on the second notification, script open 2 different tabs with first link and with second link. The same situation with 3rd, 4th, 5th .... notification: in the next time script open +1 previous link.
There is a responsible fragment of source code:
var myNotificationID = null;
chrome.notifications.create(
'name-for-notification', {
type: 'basic',
iconUrl: 'yes.png',
title: "CONTENT WAS PUBLISHED",
requireInteraction: true,
message: "Link: " + urlchk,
buttons: [{
title: "GOT IT!"
}]
},
function(id) {
myNotificationID = id;
}
);
//
chrome.notifications.onButtonClicked.addListener(function(notifId, btnIdx) {
if (notifId === myNotificationID) {
if (btnIdx === 0) {
chrome.notifications.clear(notifId);
}
}
});
chrome.notifications.onClicked.addListener(function(notifId) {
if (notifId === myNotificationID) {
chrome.tabs.create({
active: true,
url: urlchk
});
chrome.notifications.clear(notifId)
}
});
I feel I need to clear smth but I don't know what exactly. Thank you for any suggestion.
Related
I have created a Chrome extension which upon selecting text, offers a context menu link to Salesforce using the selected text:
function doSearch (search_target, tab)
{
chrome.tabs.create( {
url : "https://my.salesforce.com/apex/BR_caseRedirectDependingLicense?number="+search_target.replace(/\D/g,''),
selected : true,
index : tab.index + 1
} );
}
function selectionHandler (info, tab)
{
doSearch( info.selectionText, tab );
}
function resetContextMenus ()
{
chrome.contextMenus.removeAll(
function()
{
var id = chrome.contextMenus.create( {
title: "Open in Salesforce",
contexts: [ "selection" ],
onclick: selectionHandler
} );
}
);
}
resetContextMenus();
The intention here is to mark ticket numbers and open them in SF quickly, and it works perfectly.
However, I was wondering if it's possible to update an open salesforce tab instead of launching a new one every time.
I have tried looking around and encountered the following sample extension:
https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/tabs/inspector/
But it doesn't seem to work at all (perhaps because it's outdated).
I would much appreciate any help/guidance on how to approach this.
Yes, you can do that, you can do it using the chrome.tabs.update(..) function, here is an example, this will update the tab in which your context menu item was clicked:
function selectionHandler (info, tab) {
chrome.tabs.update(tab.id, {
url : "https://my.salesforce.com/apex/BR_caseRedirectDependingLicense?number="+info.selectionText.replace(/\D/g,''),
active : true
});
}
If you want to first create a new tab and then keep updating it, you can use something like this:
let tabId = -1;
function doSearch (search_target, tab)
{
const tabDetails = {
url : "https://my.salesforce.com/apex/BR_caseRedirectDependingLicense?number="+search_target.replace(/\D/g,''),
active : true,
index : tab.index + 1
};
if (tabId == -1) {
chrome.tabs.create(tabDetails, tab => {
tabId = tab.id;
});
} else {
// check if tab is still around
chrome.tabs.get(tabId, (tab) => {
if (tab) {
chrome.tabs.update(tab.id, tabDetails);
} else {
chrome.tabs.create(tabDetails, tab => {
tabId = tab.id;
});
}
});
}
}
Here is the chrome.tabs API documentation beside this two examples, you may also want to look into chrome.tabs.query(..), you can use that to find a specific tab.`
Also, in all these examples I've used active instead of selected because selected is deprecated.
I tried the following code. It basically takes a screenshot from all tabs open in the current window:
function captureWindowTabs(windowId, callbackWithDataUrlArray) {
var dataUrlArray = [];
// get all tabs in the window
chrome.windows.get(windowId, { populate: true }, function(windowObj) {
var tabArray = windowObj.tabs;
// find the tab selected at first
for(var i = 0; i < tabArray.length; ++i) {
if(tabArray[i].active) {
var currentTab = tabArray[i];
break;
}
}
// recursive function that captures the tab and switches to the next
var photoTab = function(i) {
chrome.tabs.update(tabArray[i].id, { active: true }, function() {
chrome.tabs.captureVisibleTab(windowId, { format: "png" }, function(dataUrl) {
// add data URL to array
dataUrlArray.push({ tabId:tabArray[i].id, dataUrl: dataUrl });
// switch to the next tab if there is one
if(tabArray[i+1]) {
photoTab(i+1);
}
else {
// if no more tabs, return to the original tab and fire callback
chrome.tabs.update(currentTab.id, { active: true }, function() {
callbackWithDataUrlArray(dataUrlArray);
});
}
});
});
};
photoTab(0);
});
}
When I call this code from popup.html opened as a webpage, it works as expected (I trigger this from a button click in the popup.html). When I call it from the browser extension, it just gets interrupted from the first tab it selects. Any idea why that is? I can't share errors, since the debugger gets closed when called from the extension.
Supplementary, is there a way to achieve desired result without needing the visual tab switching?
While updating the next tab as active tab. make sure current tab is no more active tab by doing
chrome.tabs.update(tabArray[i-1].id, { active: false }, ()=>{});
Moving the extension to a background script fixed the problem.
Reasoning is that the popup will close once the tab switches. Hence it is required to run in the background where it is not interrupted when the popup closes.
I'm trying to implement cordovaAppRate in an Ionic app, i already set the configs, and the dialog box with the options "Remind me later", "Rate now", and "No thanks" is successfully displayed, But when i click the link "Rate now", nothing happens!
I'm testing with an actual device with Android 5.1.1
here's my cordovaAppRate configs
.config( function($cordovaAppRateProvider) {
var preferences = {
language: 'de',
appName: 'MyAppName',
openStoreInApp: false,
androidURL: 'market://details?id=myapp.id',
iosURL: 'myapp.id'
}
document.addEventListener("deviceready", function() {
$cordovaAppRateProvider.setPreferences(preferences);
}, false);
})
This is the function that triggers the dialog box
$scope.rate = function() {
if (typeof AppRate != 'undefined') {
$cordovaAppRate.promptForRating(true);
};
}
any idea of why the "Rate now" link is not working?
Skip the if clause - wrap instead in ionicPlatform.ready() function.
Be sure to know how to implement plugins in the ionic-framework. Some common pitfalls can be read about here: http://ngcordova.com/docs/common-issues/
An alternative is to use this callback:
AppRate.preferences.callbacks.onButtonClicked = function(buttonIndex) {
};
In this function, check what the buttonIndex of "Rate Now" button is (0, 1, or 2), and then use
$cordovaAppRate.navigateToAppStore() to manually navigate to the app store
Is there a setting that will allow me to keep a Notification open until a user clicks it?
if (("Notification" in window)) {
Notification.requestPermission(function() {
var notification =
new Notification('Hello',
{
body : 'Hello',
icon: 'https://www.domain.com/images/live_chat_icon.png',
tag: 'Test' ,
});
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('https://www.domain.com', '_blank');
}
window.navigator.vibrate(500);
});
}
According to the docs there is a boolean requireInteraction:
A Boolean indicating that on devices with sufficiently large screens, a notification should remain active until the user clicks or dismisses it.
https://developer.mozilla.org/en-US/docs/Web/API/notification
new Notification('Hello', {
body : 'Hello',
requireInteraction: true
});
Tested in Chrome on MacOS.
I'm currently using this implementation to use Browsers based Notifications :
https://developer.mozilla.org/en-US/docs/Web/API/Notification
This works like a charm.
if ("Notification" in window) {
if(Notification.permission === "granted") {
if($('#notify-on-message').is(':checked')) {
var notification = new Notification(username + ' : ' + data, {'icon': "/custom/favicon.gif"});
}
if ($('#notify-on-hl').is(':checked')) {
var patt = new RegExp("(^|\\W)"+selfusername+"(\\W|$)");
if(patt.test(data)) {
var notification = new Notification(username + ' highlighted you.', {'icon': "/custom/favicon.gif"});
}
}
}
}
The main issue I have is that on chrome based browsers, the notification just doesn't close itself after the 3 seconds delay.
It tried adding this after the var notification = ...
setTimeout(function() {
notification.close();
}, 2000);
Though that doesn't change a single thing. The notification remains.
Is it a known issue ? Is there an easy way to fix this behaviour I don't want ?
EDIT 1:
According to this page :
https://developer.mozilla.org/en-US/docs/WebAPI/Using_Web_Notifications
This is a known issue :
Note: Firefox and Safari close the notifications automatically after a few moments, e.g. 4 seconds.
This can also be done at the web application level using the Notification.close() method, for example with the following code:
var n = new Notification("Hi!");
n.onshow = function () {
setTimeout(n.close, 5000);
}
Though that code doesn't work. There is an error in the console that says that the notification doesn't have the close method or something like that.
Well actually I was wrong, the code
var message_notification = new Notification("Data");
setTimeout(function(){
message_notification.close();
}, 3000);
Works in both Firefox and Chrome. (And Safari too I guess)
Adding a tag to the option will close active popup before showing new once
var options = {
body: msg,
icon: "logo.png",
dir: "ltr",
tag: "group1"
};