|
// ==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(); |
|
})(); |
The latest version adds a brief beep for feedback. If I have time later, I will replace the beep with a fading popup.