Dealing with a very strange gChrome / Javascript issue

I am working on a Chrome extension where I need to click on <input type='file' /> in javascript via jQuery to display the Save As dialog box. Yes, they told me - most browsers do not allow you to do this. However, I found something very strange - I can click on the file entry element (and open the "Save" popup) if I do this through the address bar, for example:

 javascript: $("<input type='file' />").appendTo("body").click(); 

So, I thought that if I could do this, then of course I could do this from my script content ...

Obviously, I was wrong. Doing $("<input type='file' />").appendTo("body").click(); from my content, the script not only makes zilch in addition to adding the element, but even emulates the address bar and makes window.location = "javascript: $(\"<input type='file' />\").appendTo(\"body\").focus().click();"; also zilch.

My initial thought was that it might have been some kind of chrome limitation in content scripts, but I'm wrong - the jsShell extension that works through a content script can run commands and get my desired results without a hitch.

So, does anyone know why jsShell and the browser can click the input file and get the “Save” dialog, but my extension cannot? I tried to parse jsShell and understand how it works (although I don't see anything special about how it works), but it STILL does not work. And the console doesn't reveal anything - no errors, no warnings.

It makes my head overheat, so any help is much appreciated!

+4
source share
1 answer

I'm not sure what the root of the problem is, but I managed to narrow it down a bit, maybe this would be useful for you.

It seems that the click event only fires when fired inside any other event. For example, this works:

 $("body").mouseover(test); function test() { $("<input type='file' />").appendTo("body").click(); } 

This also works:

 $("body").append($("<a/>").text("test").click(test)); 

But this is not so:

 $("body").append($("<a/>").text("test").click(function(){ setTimeout(test,1000); })); 

Jsshell evaluates your script with a button click, so it works, if you try to evaluate this script without a button click, it will not work.

I assume that this is a security function that allows events to be triggered only when the user performs some action, and not from some background process.

+1
source

Source: https://habr.com/ru/post/1343033/


All Articles