简体   繁体   English

使用书签获取访问页面的 url

[英]get the url of the visited page using a bookmarklet

How can I get the actual url of the visited page using a bookmarklet?如何使用书签获取访问页面的实际 url?

The idea is to click on a bookmark and the script in the bookmark returns the url.这个想法是点击一个书签,书签中的脚本返回 url。

This was a function available in Plex some time ago as a "watch later" feature to save a playlist.这是前一段时间 Plex 中提供的一项功能,作为保存播放列表的“稍后观看”功能。 So it is working, but I don't know how.所以它正在工作,但我不知道如何。

javascript:(()=>{console.log(location.href);})();

location.href holds the current URL string. location.href保存当前 URL 字符串。


Edit in response to question in comment编辑以回答评论中的问题

Any idea how I can send it to php like this: script.php?url=this_url知道如何将它发送到 php: script.php?url=this_url

or或者

How can I send the current URL as search parameter value to another URL?如何将当前 URL 作为搜索参数值发送到另一个 URL?

Let's say you want to send it to https://example.com?script.php?url= <the current URL> .假设您想将其发送到https://example.com?script.php?url= <the current URL> You'd just set location.href to the new URL and use encodeURI to encode the current URL (from location.href ).您只需将location.href设置为新 URL 并使用encodeURI对当前 URL 进行编码(来自location.href )。 (Kind of meta, right?) This is what that would look like: (有点像元,对吧?)这就是它的样子:

javascript:(()=>{location.href=`https://example.com/script.php?url=${encodeURI(location.href)}`;})();

In fact, I'll make it even easier for you.事实上,我会让你更轻松。 Just expand and run the code snippet below, and you can input the URL that you want to send it to (eg https://example.com/script?url= ) and you'll see the JavaScript bookmarklet update as you type.只需展开并运行下面的代码片段,您就可以输入要将其发送到的 URL(例如https://example.com/script?url= ),您将在键入时看到 JavaScript 书签更新。

 (() => { 'use strict'; const copyTextToClipboard = text => new Promise(async (resolve, reject) => { const fallback = text => { const textArea = document.createElement("textarea"); textArea.value = text; textArea.style.position='fixed'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { const successful = document.execCommand('copy'); successful ? resolve(true) : reject(new Error('"document.execCommand(\\'copy\\')" failed')); } catch (err) { reject(err); } finally { document.body.removeChild(textArea); } }; if (!navigator.clipboard) { fallback(text); return; } try { await navigator.clipboard.writeText(text); resolve(true); } catch (err) { reject(err); } }); const input = document.getElementById('input'); const output = document.getElementById('output'); const copyButton = document.getElementById('copy'); const makeBookmarklet = string => `javascript:(()=>{location.href=\\`${string}\\${encodeURI(location.href)}\\`;})();`; const updateOutput = ev => { output.textContent = makeBookmarklet(ev.target.value.trim()); }; const placeholder = 'https://example.com/script.php?url='; input.placeholder = placeholder; output.textContent = makeBookmarklet(placeholder); input.addEventListener('input', updateOutput); copyButton.addEventListener('click', () => copyTextToClipboard(output.textContent)); })();
 * { box-sizing: border-box; } #input, #output, #copy { border-radius: 0.25em; padding: 0.5em; } #input { font-family: monospace; display: block; width: 100%; } #output { background-color: lightgrey; padding: 1em; } #copy { background-color: grey; color: white; cursor: pointer; } pre { font-size: 1.2em; /* height: 5em; */ margin: 1em 0 0.5em; overflow-x: auto; padding: 1em 0; }
 <div> <input id="input" type="text" /> <pre><code id="output"></code></pre> <div><button id="copy">Copy bookmarklet</button></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM