5

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?.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
DoSMaN
  • 59
  • 1
  • 4

3 Answers3

9

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.

Shugar
  • 5,269
  • 1
  • 9
  • 9
  • It worked like a charm...!!! Im not quite expert with javascript so i had some errors on the code i tried to use... Your code worked!!! Thanks a lot!!! – DoSMaN Mar 16 '18 at 10:35
  • Glad to know that it's alright. However next time don't forget to post the code you've got some problems with. – Shugar Mar 28 '18 at 10:56
  • window.open is the key part to open in the new tab here – Matthew Nov 01 '18 at 05:43
  • 1
    The promise here is unnecessary. You could pull all the code out of the promise and it would still work the same. – Chrift Jun 06 '19 at 14:21
6

@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)
})();
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Chrift
  • 327
  • 3
  • 10
2

javascript:void(window.open('http://www.URL.com/'+prompt ('Enter your Query:')));

I hope this helps. Works for me and is much simpler code than I see above. (as long as we reach the end result, that is all that matters right?)

Jon Clegg
  • 11
  • 1