Skip to content

Instantly share code, notes, and snippets.

@svidgen
Last active May 25, 2021 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svidgen/08ee83a8729c8a4a45069580be920fa2 to your computer and use it in GitHub Desktop.
Save svidgen/08ee83a8729c8a4a45069580be920fa2 to your computer and use it in GitHub Desktop.
copy page link with optional text selection

Install

  1. Copy the minified bookmarklet code.
  2. Add a page/link to your bookmarks toolbar.
  3. Paste the code.
  4. Save.
  5. Profit.

Basic Usage

  1. Click the bookmark.
  2. Paste the link.
  3. Profit.

Advanced Usage

  1. Highlight page text.
  2. Click the bookmark.
  3. Paste the link + text.
  4. Profit.
javascript:var a=encodeURI(document.location.href),b=window.getSelection().toString(),c='<a href="'+a+'">'+document.title+"</a>",d=document.createElement("div");d.innerHTML=b?"<div>"+c+":<br />"+b+"</div>":c;document.body.appendChild(d);window.getSelection().removeAllRanges();var e=document.createRange();e.selectNode("string"===typeof d?document.getElementById(d):d);window.getSelection().addRange(e);document.execCommand("copy");window.getSelection().removeAllRanges();document.body.removeChild(d);var f={},g=void 0===f.frequency?1E3:f.frequency,h=void 0===f.start?0:f.start,k=void 0===f.stop?.05:f.stop,l=void 0===f.context?new AudioContext:f.context,m=l.createOscillator(),n=l.createGain();n.gain.setValueAtTime(0,h);n.gain.linearRampToValueAtTime(1,h+.0025);n.gain.linearRampToValueAtTime(0,k-.005);m.frequency.value=g;m.connect(n);n.connect(l.destination);m.start(h);m.stop(k);
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==
(function() {
function copyMarkupToClipboard(markup) {
let el = document.createElement('div');
el.innerHTML = markup;
document.body.appendChild(el);
copyElementToClipboard(el);
document.body.removeChild(el);
}
function copyElementToClipboard(element) {
window.getSelection().removeAllRanges();
let range = document.createRange();
range.selectNode(typeof element === 'string' ? document.getElementById(element) : element);
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
}
function makeLink() {
let href = encodeURI(document.location.href);
let title = document.title;
let body = window.getSelection().toString();
let link = `<a href="${href}">${title}</a>`;
if (body) {
return `<div>${link}:<br />${body}</div>`;
} else {
return link;
}
}
function beep({frequency = 1000, start = 0, stop = 0.05, context = new AudioContext()} = {}) {
let o = context.createOscillator();
let g = context.createGain();
g.gain.setValueAtTime(0, start);
g.gain.linearRampToValueAtTime(1, start + 0.0025);
g.gain.linearRampToValueAtTime(0, stop - 0.005);
o.frequency.value = frequency;
o.connect(g);
g.connect(context.destination);
o.start(start);
o.stop(stop);
};
copyMarkupToClipboard(makeLink());
beep();
})();
@svidgen
Copy link
Author

svidgen commented Mar 25, 2021

The latest version adds a brief beep for feedback. If I have time later, I will replace the beep with a fading popup.

@svidgen
Copy link
Author

svidgen commented May 21, 2021

New version softens the feedback beep and fixes waveform cutting at the start and/or end of the beep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment