3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

閲覧中のWebページのリンクをコピーするBookmarklet

Last updated at Posted at 2020-06-24

SlackやGithubでリンクを共有する際に面倒だなーと思ったところ、同僚も面倒と思ってたらしいので用意してみました.

bklet_cp_link.gif

cp link

リッチテキストのリンクをクリップボードにコピーします

  • タイトルに#から始まる数字があればそこのみリンク
  • タイトルがない場合はURLがリンク名
javascript:(c=>{const d=b=>c.createElement(b),e=(c,a)=>c.appendChild(a),f=(d,a)=>e(d,c.createTextNode(a)),g=c.body,h=location.href,i=c.title,j=i.match(/(?<=\W|^)(?=\W|$)#\d+/),k=d("span"),l=d("a");l.href=h,j?(f(l,j[0]),f(k,i.slice(0,j.index)),e(k,l),f(k,i.slice(j.index+j[0].length))):(f(l,i||h),e(k,l)),e(g,k),c.getSelection().selectAllChildren(k),c.execCommand("copy"),g.removeChild(k)})(document);
Show source code
((doc) => {
  const createNode = (a) => doc.createElement(a),
    appendNode = (a, b) => a.appendChild(b),
    appendText = (a, b) => appendNode(a, doc.createTextNode(b)),
    body = doc.body,
    url = location.href,
    title = doc.title,
    result = title.match(/(?<=\W|^)(?=\W|$)#\d+/),
    base = createNode("span"),
    anchor = createNode("a");
  anchor.href = url;
  if (result) {
    appendText(anchor, result[0]);
    appendText(base, title.slice(0, result.index));
    appendNode(base, anchor);
    appendText(base, title.slice(result.index + result[0].length));
  } else {
    appendText(anchor, title || url);
    appendNode(base, anchor);
  }
  appendNode(body, base);
  doc.getSelection().selectAllChildren(base);
  doc.execCommand("copy");
  body.removeChild(base);
})(document);

cp #link

cp link とほぼ同じ. クリップボードにコピーされるのは一部だけになる.
GithubのIssueのリンクとかに使ってます.

javascript:(c=>{const d=(c,a)=>c.appendChild(a),e=(e,a)=>d(e,c.createTextNode(a)),f=c.body,g=location.href,h=c.title,i=h.match(/(?<=\W|^)(?=\W|$)#\d+/),j=(b=>c.createElement(b))("a");j.href=g,i?e(j,i[0]):e(j,h||g),d(f,j),c.getSelection().selectAllChildren(j),c.execCommand("copy"),f.removeChild(j)})(document);
Show source code
((doc) => {
  const createNode = (a) => doc.createElement(a),
    appendNode = (a, b) => a.appendChild(b),
    appendText = (a, b) => appendNode(a, doc.createTextNode(b)),
    body = doc.body,
    url = location.href,
    title = doc.title,
    result = title.match(/(?<=\W|^)(?=\W|$)#\d+/),
    anchor = createNode("a");
  anchor.href = url;
  if (result) {
    appendText(anchor, result[0]);
  } else {
    appendText(anchor, title || url);
  }
  appendNode(body, anchor);
  doc.getSelection().selectAllChildren(anchor);
  doc.execCommand("copy");
  body.removeChild(anchor);
})(document);

cp md link

Markdownのリンクをクリップボードにコピーします

javascript:((a)=>{const b=a.createElement("span");b.appendChild(a.createTextNode("["+a.title+"]("+location.href+")"));a.body.appendChild(b);a.getSelection().selectAllChildren(b);a.execCommand("copy");a.body.removeChild(b);})(document);

おまけ

それほど複雑じゃないコードだったので、最初からminifyしたショートコードを書いてたけど、ちょいちょいカスタマイズしたくなったので、JSCompress を使って変換するようにした

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?