How can I get the actual url of the visited page using a bookmarklet?
The idea is to click on a bookmark and the script in the bookmark returns the url.
This was a function available in Plex some time ago as a "watch later" feature to save a playlist. So it is working, but I don't know how.
javascript:(()=>{console.log(location.href);})();
location.href
holds the current URL string.
Edit in response to question in comment
Any idea how I can send it to php like this:
script.php?url=this_url
or
How can I send the current URL as search parameter value to another URL?
Let's say you want to send it to 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
). (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.
(() => { '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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.