Search code examples
javascriptbookmarklet

Javascript Bookmarklet Duplicating URI / URL


I have the following bookmarklet:

javascript:findlink=document.getElementsByClassName(%22download_link%22)[2].href;window.open('https://myfiledrive.com/users/files/add?url='+findlink,'_blank');void(0);

Example:

<a class="download_link" href="example.com/pdf1.pdf">
<a class="download_link" href="example.com/pdf2.pdf">
<a class="download_link" href="example.com/pdf3.pdf">

Basically, it searches the currently active page, for the third iteration of an tag with the class "download_link", and stores it in the variable "findink",

Then it loads 'https://myfiledrive.com/users/files/add?url='+findlink

In the above example it should load:

https://myfiledrive.com/users/files/add?url=example.com/pdf3.pdf

but what ends up happening is that this gets loaded:

https://myfiledrive.com/users/files/add?url=example.com/pdf3.pdf?url=example.com/pdf3.pdf

  • so basically it's duplicating url=

What am I doing wrong? Thanks.


Solution

  • The url param shouldn't be duplicated. You're not appending to findlink or anything. You can try the snippet below, it's exactly as you've posted.

    Chrome will block the popup, but if you read the error message, there's no duplication going on:

    Blocked opening 'https://myfiledrive.com/users/files/add?url=https://stacksnippets.net/example.com/pdf3.pdf' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.

    url is only in there once no matter how many times I click.

    <a class="download_link" href="example.com/pdf1.pdf">1</a>
    <a class="download_link" href="example.com/pdf2.pdf">2</a>
    <a class="download_link" href="example.com/pdf3.pdf">3</a>
    
    <a href="javascript:findlink=document.getElementsByClassName(%22download_link%22)[2].href;window.open('https://myfiledrive.com/users/files/add?url='+findlink,'_blank');void(0);">download</a>