[ブックマークレット] ページタイトルと URL をコピーする

以前見ているサイトのページタイトルと URL をコピーするブックマークレットを紹介した。

[Chrome] ページタイトルと URL をコピーする – with a Christian Wife
https://wacw.cf/2018/12/12/copy-title-and-url-in-chrome/

[Chrome] ページタイトルと URL をコピーする2 – with a Christian Wife
https://wacw.cf/2018/12/13/copy-title-and-url-in-chrome-2/

[iOS] ページタイトルと URL をコピーする – with a Christian Wife
https://wacw.cf/2018/12/30/copy-title-and-url-in-chrome-ios/

最近は Markdown を勉強し始めたので、Markdown 用のリンクを作成できるブックマークレットを新たに考えた。

2パターン作り、1は iOS で動かないが、2は iOS でも動く。2を使う方がパソコンでも iOS でも使えて便利だ。

(1)iOS で動かないバージョン

javascript:var global=window;global.COPY_TO_CLIPBOARD=global.COPY_TO_CLIPBOARD||{};global.COPY_TO_CLIPBOARD.getUrlInfo=function(){var a=new String(document.title);a.allReplace=function(a){var b=this,c;for(c in a)b=b.replace(new RegExp(c,"g"),a[c]);return b}.bind(a);return"["+a.allReplace({":":"\uff1a","\\[":"\uff3b","\\]":"\uff3d"})+"]("+document.URL+")"};
global.COPY_TO_CLIPBOARD.copyToClipboard=function(){var a=document.createElement("textarea");a.textContent=this.getUrlInfo();var d=document.getElementsByTagName("body")[0];d.appendChild(a);a.select();var b=document.execCommand("copy");d.removeChild(a);return b};global.COPY_TO_CLIPBOARD.copyToClipboard();

(2)iOS でも動くバージョン

javascript:(function(){function copy(e){var t=document.createElement("span");t.style="-webkit-user-select: auto;",t.textContent=e,document.body.appendChild(t);var n=document.createRange();n.selectNode(t);var o=window.getSelection();o.removeAllRanges(),o.addRange(n),document.execCommand("copy"),setTimeout(function(){t.remove()},50)}copy("["+document.title+"]("+document.URL+")");})();

これで [ページタイトル](URL) の形式でクリップボードにコピーされる。