I am first getting into Chrome Extensions. I have a browser_action that uses default_popup to open a tiny HTML page with a button. The JS used on that page is:
document.addEventListener('DOMContentLoaded', () => {
var shareButton = document.getElementById('share');
shareButton.addEventListener('click', () => {
chrome.desktopCapture.chooseDesktopMedia(["screen","window"], (streamId, options) =>{
});
});
});
When I hit the button, I see the OS window pop to ask what I want to share for a fraction of a second before it instantly closes.
Related
Fragment that I have problem with:
const afterPrint = () => {
this.location.back();
window.removeEventListener('afterprint', afterPrint);
};
window.addEventListener('afterprint', afterPrint);
window.print();
My goal is to redirect user back in history after print dialog has been closed (print or cancel)
On Chrome it works as it should but on Firefox 89 afterprint event is fired when page is dumped for printing when print dialog is still open.
Because the print dialog is still open, this.location.back() is blocked and doesn't work.
Tried it with firefox 83 previously and it worked as intended but after update I have noticed that it is not the case anymore.
Is there any workaround for it?
I have found a solution if anyone is curious
My solution was to place this.location.back(); in window.setTimeout which is blocked from execution while print dialog is open.
After closing the print dialog I am redirected to the previous page as intended.
code:
const afterPrint = () => {
window.setTimeout(() => {
this.location.back();
window.removeEventListener('afterprint', afterPrint);
}, 1);
};
window.addEventListener('afterprint', afterPrint);
I am currently writing a Firefox extension that opens a new window in my background script. When a user clicks a button on a page the background script executes and opens the window.
So far I have:
// content script
downloadMod(linkToOpen); //button clicked
function downloadMod(url) {
// var test = window.open(url, '_blank');
var myPort = browser.runtime.connect({ name: "cac410c4672fff93bf0d3186636d8876de3dfeb6#temporary-addon"});
myPort.postMessage({ greeting: url });
}
In my background script (the script the above code connects too) I have:
//background script
portFromCS.onMessage.addListener(function (m) {
var test = browser.windows.create({
url: m.greeting,
allowScriptsToClose: true,
});
NOTE: all the code works as expected. However, the trouble comes when closing the window.
I have tried variations of self.close(), window.close(). I even created a function to wait 5 seconds to load the newly created window and then close the page to no avail. All of my attempts come back with the error:
Scripts may not close windows that were not opened by script.
I thought this error was supposed to be removed with the allowScriptsToClose flag?
To close a window you created with browser.windows.create, you need to use browser.windows.remove.
So:
let windowId;
browser.windows.create({url: "http://google.be"}).then((data) => {
windowId = data.id;
});
// Sometime later, use windowId to close the window
setTimeout(function(){
browser.windows.remove(windowId);
}, 5000);
I have the following JS code that helps signing out a session from an ASPX page so a 'log out' record can be stored in a database. The code works when the site is opened with IE11, EDGE, and Chrome. Using those three browsers the user gets the popup asking if staying on the site or leaving the site. When it comes to be tested in Firefox, at least version 54, it just does not work. Nothings pops up.
Code is:
//We want to capture when a user logs out from the system - this is a way to force the browser to log out the session
window.onbeforeunload = closingCode;
function closingCode() {
// when user clicks the 'X' from the browser, will be prompted to leave or to stay
//clicking any of the options will trigger an event that will capture when the session was logged out and saved in the db
var button = document.getElementById('btnLogout');
button.click();
return "You are closing or refreshing this site. Clicking 'Stay on this page' will log out your session. Clicking 'Leave this page' will close your window.";
//return null;
}
//this will prevent the popup to appear everytime user navigates between pages/links
$(document).ready(function () {
$('a[rel!=ext]').click(function () {
window.onbeforeunload = null;
});
$('form').submit(function () {
window.onbeforeunload = null;
});
});
We have a Chrome extension download button on our site.
When you click it, it opens a popup that says 'Add extension', 'Cancel' etc.
http://i.imgur.com/RFuts0E.png
The image shows the popup I'm referring to.
It works fine, except for the cancel button opens a new tab and takes you to the plugins chrome store page.
I have no idea why it does this, or how to just get it to cancel.
The js:
chrome.webstore.install(webStoreURL, () => null, (error, errorCode) => {
window.open(PLUGIN_LINKS.Chrome, '_blank');
});
Any help is much appreciated.
Well, you indiscriminately try to open the Webstore page on "error". In fact, user clicking Cancel is one of many "error" conditions.
You need to analyze the errorCode to filter that out.
chrome.webstore.install(webStoreURL, () => null, (error, errorCode) => {
if (errorCode !== "userCanceled") {
window.open(PLUGIN_LINKS.Chrome, '_blank');
}
});
Note: as is obvious from the error code list, there are many other conditions that make opening the Web Store page useless. You should re-think this logic.
I have a page action popup that works when loaded from the manifest file. However I want to get the tab information for the tab that was clicked to launch the popup. I can get the tab information from chrome.pageAction.onClicked.addListener but I don't know how to launch popup.html from inside pageAction.onClicked.
You cannot have both a pageAction.onClicked:
onClicked
This event will not fire if the page action has a popup.
What you can do, though, is fetching the current tab information with the Tabs module when the popup is loaded:
chrome.tabs.getCurrent(function(tab) {
// tab contains information about the current tab
});
I found a workaround for what I wanted to do. On the background page:
chrome.tabs.onActiveChanged.addListener(OnActiveChanged);
function OnActiveChanged( tabId, selectInfo )
{
chrome.tabs.get( tabId, function( tab ){
window.activeTab = tab;
} );
}
This captures the tab each time the tab changes. Then in my popup's function gets the tab from the background page:
function OnLogin( )
{
backgroundWindow = chrome.extension.getBackgroundPage();
var activeTab = backgroundWindow.activeTab;
...
}
Be careful when you debug the code though. The debugger causes a tab change event which changes the tab away from the tab that launched the popup.