DoSMaN
DoSMaN

Reputation: 59

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

Upvotes: 5

Views: 8926

Answers (3)

Jon Clegg
Jon Clegg

Reputation: 11

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

Upvotes: 2

Chrift
Chrift

Reputation: 345

@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)
})();

Upvotes: 7

Shugar
Shugar

Reputation: 5299

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.

Upvotes: 9

Related Questions