Skip to content

Instantly share code, notes, and snippets.

@hidao80
Last active September 3, 2023 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidao80/0a67c47377260322d97bc43a20529156 to your computer and use it in GitHub Desktop.
Save hidao80/0a67c47377260322d97bc43a20529156 to your computer and use it in GitHub Desktop.
javascript:(function(){function t(t){const n=t.split(s);let e="";for(let t of n)t=t.trim(),t==n.length-1&&0==t.length||(e+="> "+t+s);return e}function n(){const n=window.getSelection();return n&&""!=n?t(o(n+s).trim()):""}function e(){const t=n(),e=document.title,o=document.URL,i=window.getSelection(),r=i?`[${c}](${o}#:~:text=${encodeURIComponent(i)})`:"";return t+s+r+s+"『"+e.trim()+"』"+s+o+s}function o(t){return t.replace(new RegExp(u,"g"),"")}const i="https://misskey.dev",c="選択範囲へ直リンク",r="\n",u="\r",s=r,d="width=600,height=400",l=encodeURIComponent(e()),m=`${i}/share?text=${l}`;window.open(m,"Notes",d)})();
/**
* Copyright (c) 2022 hidao80
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
(function () {
const instance = "https://misskey.dev";
const link_text = "選択範囲へ直リンク";
const LF = '\n';
const CR = '\r';
const BR = LF;
const CSS_SIZE_POPWINDOW = 'width=600,height=400';
function addHeaderQuote(str) {
const lines = str.split(BR);
let result = '';
for (let line of lines) {
line = line.trim();
if (!(line == lines.length - 1 && line.length == 0)) {
result += '> ' + line + BR;
}
}
return result;
}
function getSelectedStringAsQuote() {
const str = window.getSelection();
return (!str || str == '') ? '' : addHeaderQuote(uniformBRtoLF(str + BR).trim());
}
function makeComment() {
const str = getSelectedStringAsQuote();
const title = document.title;
const url = document.URL;
const selection = window.getSelection();
const link = selection ? `[${link_text}](${url}#:~:text=${encodeURIComponent(selection)})` : '';
return str + BR + link + BR + '『' + title.trim() + '』' + BR + url + BR;
}
function uniformBRtoLF(str) {
return str.replace(new RegExp(CR, 'g'), '')
}
const str = encodeURIComponent(makeComment());
const title = `${instance}/share?text=${str}`;
window.open(title, 'Notes', CSS_SIZE_POPWINDOW)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment