LoginSignup
1
2

More than 5 years have passed since last update.

cVim でページのタイトルとURLをコピー(リッチテキスト形式も)

Last updated at Posted at 2019-01-20

cVim はVimライクなキーボード操作ができるようになるChromeの拡張です。
cVim - Chrome ウェブストア

下記のような設定で、ページのタイトルとURLをコピーするキーバインドを定義できます。
自分はよくMarkdown形式でコピーします。便利。

" Clipboard Copy
copyText (text) -> {{
  Clipboard.copy(text);
  Status.setMessage('copied: ' + text, 2);
}}
copyUrl (format) -> {{
  let text = format.replace("%URL%", location.href).replace("%TITLE%", document.title);
  Clipboard.copy(text);
  Status.setMessage('copied: ' + text, 2);
}}
" copy markdown link
map cm :call copyText('['+document.title.replace(/([|\[\]])/g,'\\$1')+']'+'('+location.href+')')<CR>
" copy org link
map co :call copyText('[['+location.href+']['+document.title.replace(/([\[\]])/g,' ')+']]')<CR>
" copy textile link
map ct :call copyUrl('"%TITLE%":%URL%')<CR>
" copy title and url
map cu :call copyUrl("%TITLE% / %URL%")<CR>
" copy scrapbox link
map cs :call copyUrl('[%TITLE% %URL%]')<CR>
" copy href link
map ca :call copyUrl('<a href="%URL%">%TITLE%</a>')<CR>

こちらはリッチテキスト形式でリンクをコピーする関数。
Range.selectNode()document.execCommand('copy') 使ったらできるかなと思い試したらできました。
WYSIWYGエディタやパワポにリンク貼り付けるときに使えます。

copyHtmlLink () -> {{
  var clipNode = document.createElement('a');
  var range = range = document.createRange();
  var sel = window.getSelection();
  clipNode.setAttribute('href', location.href);
  clipNode.innerText = document.title;
  document.body.appendChild(clipNode);
  range.selectNode(clipNode);
  sel.removeAllRanges();
  sel.addRange(range);
  document.execCommand('copy', false, null);
  document.body.removeChild(clipNode);
  Status.setMessage('copyHTMLLinkToClipboard',2);
}}

" copy html link
map cc :call copyHtmlLink()<CR>

Qiita で実行してテキストエディットに貼り付けるとこんな感じです。
上がMarkdownで下がリッチテキスト形式。
SS_ 2019-01-20 19.26.38.png

cVimの設定ファイルはこちらに置いています。
https://gist.github.com/hushin/f1a3d90b9db63026e798a90795733c6b

参考にした記事

1
2
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
1
2