Is it possible to call the function defined in the bookmarket from the page level of the script?

I have a bookmarklet that needs to open a new window / tab. To avoid blocking pop-ups, I need to call the window.open() method directly in the bookmarklet, that is: at the browser level.

However, I want to keep the updated bookmarklet by loading external Javascript files. To do this, the booklet booklet must add nodes to the wm script. If I were to put the window.open() code in one of these external downloadable scripts, the popup blocker would block it from page level.

What do I want to know if I can create a wrapper function around window.open() in my bookmarklet and then call it from an externally loaded script? What is the scope and permissions for such a transfer?

+4
source share
2 answers

I came up with a solution that is not perfect, but meets the requirements:

Here is the bookmarklet code:

 javascript:window.open(window.location);window.location="http://www.google.com/";var%20s=document.createElement('script');s.setAttribute('src','http://my-script.js');document.body.appendChild(s);void(0); 

Readable stepwise equivalent:

 window.open(window.location); // Clone the current tab window.location = "http://www.google.com/"; // Navigate to the desired page url var s = document.createElement('script'); // Create the script s.setAttribute('src','http://my-script.js'); // document.body.appendChild(s); // Embed it into current document 

There is only one problem left: the page you want to show is inactive by default. Cloned.

+1
source

I wondered if this approach could work - it's good to see that he does it.

A common problem is that browsers will not allow you to open a new window other than direct user interaction. Thus, you cannot open a window from a remote device script.

You open a window directly from the bookmarklet, moving to this place, and then call the remote script.

The alternative I went with was moving the contents of the remote script directly to the bookmarklet. This was good for my simple application. I wrote that on my blog

0
source

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


All Articles