Crossrider setPopup not callling more then once - browser

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

Related

Open another page in Dialog box

I am trying to use Dialog box.
In my add-in, I have two buttons openGoogle and openStackoverflow which link to openDialog("https://www.google.com") and openDialog("https://www.stackoverlfow.com"):
function openDialog(url) {
Office.context.ui.displayDialogAsync(url, { height: 1100, width: 1000 },
function (result) {
console.log("here")
dialog = result.value;
}
)
}
First, I open one dialog by clicking on openGoogle, then if I click on openStackoverflow, here is displayed again, however the dialog does NOT go to stackoverflow.
Does anyone know what's wrong?
From the documentation:
the page, controller method, or other resource that is passed to the
displayDialogAsync method must be in the same domain as the host page.
If you need to get users to another domain, you can do this by first opening a dialog to a page within your add-in that then immediately redirects them to the external domain using window.location.href = "https://www.stackoverlfow.com";

Chrome extension keeps resetting

I'm new to chrome extension development but I'm running into issues debugging and with the extension itself. I currently have a form with a submit button and after the user hits submit I change the html to show the data that was just submitted.
document.addEventListener('DOMContentLoaded', function() {
console.log("1");
document.getElementById("submitButton").addEventListener('click', myFunction);
console.log("2");
//getCurrentTabUrl(function(url) {
// renderStatus(url);
//});
});
function myFunction() {
document.getElementById("formOutput").innerHTML = document.getElementById("formInput").value;
alert("hi");
console.log("test");
);
}
In this, 1 and 2 are displayed to the extension debugging console and the alert runs as well when the button is clicked. The test shows up very briefly and then it disappears.The formoutput value changes very briefly as well and then changes back to the default value I have. Does anyone know why my code/the chrome extension would be doing this?
Thanks!
When button with type submit (This is the default if the attribute is not specified) is clicked, the form data will be sent to server and page will be redirected to new page. Then your content script will be injected again, that's why the it changes back to default value.
To avoid this, you could change button with other types, or calling e.preventDefault to prevent default behavior. Then you would want to use Ajax to send data to server, which ensure the whole page won't be redirected and only parts of UI can be updated.

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.

chrome extension background script confirm message title

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

what is the use of window.open('','_self').close(); in chrome extension

When I click the extension icon, a popup is shown.
After that, when I try to click "URL restrictions", it will open a window, after that when I click the popup again, the popup is overlapping that url restriction window.
The above issue happens on Windows only, not on Linux.
So I have added window.open('','_self').close(); which apparently fixed the issue. But not exactly. Is it correct? I have referred this Link and Link2 but can not understand the meaning.
What is the purpose of window.open('','_self').close();?
EDIT: this is my popup.js
function click(e) {
var windowObj = window.open(site_exception_url, 'url_window', params);
windowObj.focus();
window.close();
window.open('','_self').close();
return false;
}
On Windows the popup isn't closed automatically after a new window is opened from a link within the popup.
Close it in the click handler manually, this won't hurt Linux but will help on Windows:
document.addEventListener("click", function(event) {
if (event.target.localName == "a") {
close();
}
});
The related questions linked in your question don't apply here as the first is for userscripts, not extensions, and the second isn't for popups shown by the browser when you click the toolbar button.

Resources