Last active
July 26, 2026 03:43
-
-
Save sendarionn/0384c1dd2fb0888ebf562fed19d75864 to your computer and use it in GitHub Desktop.
選択した文字列とURLをCosneseのリンク記法でコピーするブックマークレット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| javascript:(async()=>{const selection=window.getSelection();const text=selection?.toString().trim();if(!text||selection.rangeCount===0)return;const range=selection.getRangeAt(0);const context=(node,offset,length,before)=>{if(node?.nodeType!==Node.TEXT_NODE)return'';const value=node.textContent;return(before?value.slice(Math.max(0,offset-length),offset):value.slice(offset,offset+length)).trim();};const prefix=context(range.startContainer,range.startOffset,20,true);const suffix=context(range.endContainer,range.endOffset,20,false);const encodeTextFragment=value=>value.replace(/[\t\n\r ]/g,c=>encodeURIComponent(c)).replace(/-/g,"%2D");const fragment=`:~:text=${prefix?`${encodeTextFragment(prefix)}-,`:''}${encodeTextFragment(text)}${suffix?`,-${encodeTextFragment(suffix)}`:''}`;const baseUrl=location.href.split('#')[0];const url=`${baseUrl}#${fragment}`;try{await navigator.clipboard.writeText(`[${url} ${text}]`);const notice=document.createElement('div');notice.textContent='コピー完了';Object.assign(notice.style,{position:'fixed',top:'20px',left:'50%',transform:'translateX(-50%)',padding:'8px 16px',background:'#333',color:'#fff',zIndex:2147483647,borderRadius:'4px',fontSize:'14px',pointerEvents:'none',boxShadow:'0 2px 8px rgba(0,0,0,.3)'});document.body.appendChild(notice);setTimeout(()=>notice.remove(),1500);}catch(error){console.error('コピーに失敗',error);}})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment