Cannot complete download from popup when prompting location with saveAs - firefox

I've been working on a browser extension allowing users to generate epub files from any webpage.
To use the extension, a user has to click on a toolbar button which opens a popup. On the latter, after specifying a config, the user clicks on "Generate". Here is my issue though:
When I use the download API directly, and specify saveAs to false (i.e the user is NOT prompted where to save the file), then the download is completed as expected and the file saved in some location.
However, when I do set saveAs to true --- which is what I want in the final version --- then once the user has selected the location under which to save the file, the download fails. My intuition is that as soon as the file location window pops up, the popup looses focus and dies, which makes the download impossible to resume.
Is that the explanation? And is there a way to prevent such a problem? I do not find my use case to be much of a stretch, and do not get why popups associated with browser actions are not allowed to loose focus for file selection.
Another weird thing is that I'm pretty confident this problem only appeared recently in firefox, since when I started developing the extension, I had no issue.

My intuition is that as soon as the file location window pops up, the
popup looses focus and dies, which makes the download impossible to
resume.
That's right...
Browser Action Popup is page and once page is closed, async operations will lose their reference. It is best to pass async operations to the background script.
You can use
runtime.sendMessage()
to pass a message to the background script to run the download.
You can also use the async
runtime.getBackgroundPage() to run a function in the background script but the first option works
easier as you dont need to wait for the async response as in runtime.getBackgroundPage()

Related

Write image to clipboard in Firefox

I'm currently trying to write an image blob data to the clipboard in Firefox (104.0 Linux).
I seeked information about Permissions, and Clipboard.
As for the permissions, I found contradictory things, some that say you should request for some clipboard write permission (https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API or https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard), some that say that the permission is temporary granted on user interaction. The later seems to work because I'm able to use navigator.clipboard.writeText("blah") to write successfuly to the clipboard.
Problem is when trying to write data. Mozilla documentation states about 2 versions :
Clipboard.write() (https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write)
Seems obsolete or something, because the method simply doesn't exist, I only have writeText()
setImageData() (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/clipboard/setImageData). This one makes absolutely no sense as browser doesn't exist, as well as setImageData() in the navigator.clipboard instance
Any help would be grealty appreciated here !
EDIT : in fact to actually gt a definition for write() method, user has to manually define a bool true value for flag dom.events.asyncClipboard.clipboardItem in about:config
So, pretty much unusable in production environment, if you don't expect power users only...
So now, I have a new issue, even with that bool set to true, firefox goes "lack of user activation" half of the time, even though the tab is focused. It seems I have to actually do something on the page in order to get it to work ; IE. move the mouse, click here and there...
I even tried to add a prompt, as my clipboard.write() occurs deep in await/async stuff under the button click handle (I'm actually in a NodeJS/Nuxt environment), and writing to the clipboard from the prompt, and even there, I get that error...

Disable cmd-R for executing scripts in Google Apps Script Editor on Mac

I've just started to use the Google Apps Scripts Editor on Chrome on a Mac. The good people at Google added a shortcut for quickly running scripts, which on Windows is ctrl-R, and probably doesn't conflict with anything. Then they translated it to cmd-R on Mac, which globally makes sense. As a Mac user however, I'm used to using cmd-R to reload pages (as compared to F5 on Windows). The google shortcut takes precedence, meaning that each time I hit cmd-R to simply force updating the page (because it sometimes help to reload the page), it launches the script I'm viewing. I'm worried it may at some point execute a script that I'm not intending to execute at that particular time.
Is there any way to disable the google shortcut? I'm perfectly fine having to go and click the arrow, but I'm not fine risking launching a script when I don't mean to.
Unfortunately, there is no way to disable the shortcut keys in Google Apps Script. However, you can add a confirmation dialog box in your script to add extra layer of protection from running your script accidentally.
Example:
Code:
function myFunction() {
var confirm = Browser.msgBox('Are you sure you want to execute the script?', Browser.Buttons.OK_CANCEL);
if(confirm == 'ok'){
//insert your code here
}
}
Dialog Box:
Reference:
msgBox(prompt, buttons
Following #Nikko's suggestion I'm displaying a dialog box when the script is started (i.e. top-level), and when the answer is "cancel" I simply wait (Utilities.sleep()) for 5 minutes, and then again. This prevents the function from being executed until the script would be killed automatically, but more adequately it keeps the script execution dialog box displayed, in which you can choose to cancel the execution. So you don't have to wait 5 minutes, just click "cancel" while the script is doing nothing. Again, dirty, but works for me.
if(Browser.msgBox("Are you sure you want to run this script?", Browser.Buttons.OK_CANCEL) == 'cancel') {
Utilities.sleep(300000)
Utilities.sleep(300000) // just to prevent accidental execution in the last split-second
}

How to start "save as / open" after requesting a file from server?

I'm using MooTools as JS-Framework.
When a user clicks the "Show Report"-Button on my website I'm starting Request.HTML to retrieve the path of the requested report.
If the report does not exists yet, its been created on the server (a waiting popup is shown to the user). The report file is saved to a special path on the server and this path is returned to the browser.
After I got the path to the Report-File - the Report can reach about 5 to 8MB - I want to show the user a "Download-ProgressBar" while starting a new Request.HTML to retrieve the big report file.
Everthing until this point works fine. I got the ProgressBar filling up, after its finished I got my big file.
But my problem is now, how I can start something like "Open File" or "Save File As" from JavaScript.
I got the freaking file downloaded and shown in the console but now I want the user to save this file somewhere or to open it directly...
Kind Regard.
Why don't just show a link to that file so that it opens in a new page?
That way one can open in a new window and save from the browser's menu (or use the right click>save to menu).
Since you already have your file serialized most browsers nowadays supports the data uri scheme so if this fine to support a limited subset of browsers you can do
window.open(fileToSave, '_blank');
notably old version of IE don't support all kind of files using data-uri so it will not work for html.
There are some File API in newer browser too, and there are some other solutions involving flash but historically the right way to do this is to navigate to a server page which returns an header
Content-Disposition: attachment; filename=yourfilename.html
that will work in any browser (I used this a lot in IE6 with no particular problem).

Selenium-IDE test can't switch browser windows

I'm writing a Selenium IDE script to test part of our website that opens a window that takes the user to a third party site that we integrate with. The test clicks a link on our website, that opens a new window on the third party site where we need to tick a checkbox and submit a form, at which point the window closes and we then need to check another page on our own website.
Our problem is that the script breaks at the Selenium selectWindow action, because it says that it cannot find a window with the given name. If we then manually run that line in the IDE, it works!
I have added numerous waits, pauses and other tricks to make sure that the window exists and has the correct title when the SelectWindow action is hit, but we still get the above error.
The script is...
click link=activate
waitForPageToLoad
selectWindow Third party activation site
Are there any tricks to getting this to work?
You dont need that waitForPageToLoad as i'm assuming the host page isn't reloading.
You may want to check out the api ( http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html#selectWindow(java.lang.String) ) entry for the command. This is the java docs, but it the same for selenium IDE.

Controlling multiple Internet Explorer windows?

We've got several web-based applications that are launched from our ERP system (SAP R/3 in this case, but since we're using ShellExec, I don't consider this a SAP issue). The problem is that we can either set the IE to "open every URL passed from the ERP system in a new window" or to "reuse any one of the existing IE windows" (same problem with tabs). Both settings are not acceptable for our users: one of the web applications is a rather bulky medical image viewer applet that wreaks havoc if invoked multiple times. The other option is a no-go either because this way opening the image browser for a patient automatically displaces the lab result display for the very same patient and vice versa.
I'd like to have some control over which window may or may not be reused. My idea would be a kind of "window tag" and a helper program that checks if a window with that tag exists - if it does, reuse it, if not create it. So basically
IELauncher SEARCH http://www.google.com
would open a new window and tag it as SEARCH.
IELauncher DEVEL http://stackoverflow.com
would open a second window, tagging it as DEVEL. Then
IELauncher SEARCH http://www.wikipedia.org
would replace the contents of the first (google) window only.
Is this possible at all? Do you have any pointers for me where to start? I don't even know what to look for...
I had to resort to some rather ugly methods - dynamically assembling a HTML page that uses JavaScript and window.open() to open and access a window by name, then close the page executing the JavaScript. Ugly, but at least this way you get the additional benefit of being able to control certain aspects of the browser window like switching of the menu bar, toolbar and location bar.

Resources