Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Bookmarklet which open in new tab with specific action

I want to make a Chrome Bookmarklet which open a new tab with specific action.

To be more exact I want to have a fixed URL like "https://www.blablabla.com/search=" inside the bookmarklet and when I press it, I want a popup window to appear with an input field.

When I type something in the input field and press enter or OK/submit it should "run" the whole link plus my query.

For example, I press the bookmarklet, the input field appears and input the word "test" (without the quotes).

When I press submit the query, a new tab will open with the address of https://www.blablabla.com/search=test as the URL.

How do I do that?

I tried with prompt function but I can't get it work...

My question is a bit similar to How do I get JavaScript code in a bookmarklet to execute after I open a new webpage in a new tab?.

like image 479
DoSMaN Avatar asked Dec 24 '22 08:12

DoSMaN


2 Answers

Although it remains unclear what exact issue you encounter, try the following bookmarklet:

javascript:(function() {
    var targetUrl = "http://www.blablabla.com/search=";
    new Promise (
        (setQuery) => {var input = window.prompt("ENTER YOUR QUERY:"); if (input) setQuery(input);}
    )
    .then (
        (query) => window.open(targetUrl + query)
    );
})();

If it doesn't work, you should provide the problem description in more detail.

like image 109
Shugar Avatar answered Jan 05 '23 17:01

Shugar


@Shugar's answer is mostly correct, but you don't need the promise.

javascript:(function() {
  var targetUrl = "http://www.blablabla.com/search=";

  var input = window.prompt("ENTER YOUR QUERY:");

  if (input)
    window.open(targetUrl + input)
})();
like image 27
Chrift Avatar answered Jan 05 '23 19:01

Chrift