Search code examples
javascripthtmlbookmarklet

Bookmarklet that uses current url in post request


Sorry if my question displays a lack of fundamental Javascripting knowledge as I have next to none.

I Frankensteined together the following bookmarklet during hours of obsessive Googling for a way to do what I want to do:

javascript:'<body onload="document.forms[0].submit()"><form method="post" action="https://generic.web.proxy/request.php?do=go"><input type="text" name="get" value=???></form>'

(Mostly based on this.)

Basically, it mostly does what it should: performing a post request on that web proxy's page with whatever string is used for value. The problem is what I want to use for value, namely the URL of the current Firefox tab. In other bookmarklet examples I've seen this seems to be accomplished with location.href, but if I do it like this

value=location.href

it simply assumes that it's the string "location.href". I assume that's because I'm stupidly trying to directly use a Javascript thingie in the html part of the script, but what is the alternative?


Solution

  • Oh boy, I think I just figured it out. Since the javascript treats the html like any other string, I can simply use normal string manipulation on it:

    '<beginningofhtml'+location.href+'endofhtml>'
    

    Applied to my bookmarklet:

    javascript:'<body onload="document.forms[0].submit()"><form method="post" action="https://generic.web.proxy/request.php?do=go"><input type="text" name="get" value='+location.href+'></form>'
    

    And it works!

    (the fake example web proxy url still needs to be replaced with the correct one of corse; it works with a site called Proxfree)