Javascript 书签复制 URI / URL

Javascript Bookmarklet Duplicating URI / URL

我有以下小书签:

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

示例:

<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">

基本上,它搜索当前活动页面,寻找带有 class "download_link" 的标签的第三次迭代,并将其存储在变量 "findink"、[=17= 中]

然后加载'https://myfiledrive.com/users/files/add?url='+findlink

在上面的例子中它应该加载:

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

但最终发生的是加载:

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

我做错了什么?谢谢。

url 参数不应重复。您没有附加到 findlink 或任何内容。您可以尝试下面的代码段,它与您发布的完全一样。

Chrome 将阻止弹出窗口,但如果您阅读错误消息,则不会发生重复:

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无论我点击多少次都只出现一次。

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