// ==UserScript== // @name AO3: Copy Work Link as HTML // @namespace https://greasyfork.org/en/users/906106-escctrl // @description copy an HTML link of the work to your clipboard // @author escctrl // @version 1.0 // @match *://*.archiveofourown.org/works/* // @match *://*.archiveofourown.org/tags/* // @match *://*.archiveofourown.org/users/*/works* // @match *://*.archiveofourown.org/users/*/bookmarks* // @grant none // @require https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js // @license MIT // @downloadURL none // ==/UserScript== (function($) { 'use strict'; $('.work.blurb .datetime, .bookmark.blurb .datetime').append(`
`); $('.work.navigation.actions').append(``); $('.copyBlurbLink').on('click', function(e) { let a = $(e.target).parent().parent().find('h4 a:first-of-type'); copy2Clipboard(e, `${$(a).text()}`); }); $('.copyWorkLink').on('click', function(e) { let link = $('.work.navigation.actions li.share a').prop('href').slice(0, -6); let title = $('h2.title.heading').text(); copy2Clipboard(e, `${title}`); }); })(jQuery); // solution for setting richtext clipboard content found at https://jsfiddle.net/jdhenckel/km7prgv4/3/ // and https://stackoverflow.com/questions/34191780/javascript-copy-string-to-clipboard-as-text-html/74216984#74216984 function copy2Clipboard(e, str) { // trying first with the new Clipboard API try { const clipboardItem = new ClipboardItem({'text/html': new Blob([str], {type: 'text/html'}), 'text/plain': new Blob([str], {type: 'text/plain'})}); navigator.clipboard.write([clipboardItem]); } // fallback method in case clipboard.write is not enabled - especially in Firefox it's disabled by default // to enable, go to about:config and turn dom.events.asyncClipboard.clipboardItem to true catch { console.log('Copy Tag to Clipboard: Clipboard API is not enabled in your browser - fallback option used'); function listener(e) { e.clipboardData.setData("text/html", str); e.clipboardData.setData("text/plain", str); e.preventDefault(); } document.addEventListener("copy", listener); document.execCommand("copy"); document.removeEventListener("copy", listener); } }