Misskey share
2022/6/17 21:20:00
(function () {
var lines = [];
window.prompt("title", "value");
let text_from = `[${document.title}](${window.location.href})`;
let div = document.createElement('div');
let sel = window.getSelection();
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
div.appendChild(sel.getRangeAt(i).cloneContents());
}
let node2mfm = (node => {
if (node.nodeName == "DIV") {
const body = Array.from(node.childNodes).map(node2mfm).join("");
if (body.slice(-1) == "\n") { return `${body}` } return `${body}\n`;
}
if (node.nodeName == "BR") {
return "\n";
}
if (node.nodeName == "UL") {
const childNodes = Array.from(node.childNodes);
const body = childNodes.map((node, index) => {
if (node.nodeName == "OL") { return `${index + 1}. ${node2mfm(node)}\n` }
if (node.nodeName == "LI") { return `- ${node2mfm(node)}\n` } return node2mfm(node);
}).join("");
return body;
}
if (node.nodeName == "A") {
const href = node.href;
return Array.from(node.childNodes).map(childNode => {
return `[${node2mfm(childNode).replace(/\n/g, "")}](${href})`;
}).join("");
}
if (node.nodeName.match(/H\d/)) {
let body = Array.from(node.childNodes).map(node2mfm).join("");
body = body.split("\n").filter(line => line.match(/[^\s]/g)).filter(line => line != "\n").map(line => `$[x2 ${line}]`).join("\n");
return `\n${body}\n`;
}
if (node.nodeName == "P") {
const body = Array.from(node.childNodes).map(node2mfm).join("");
return `\n${body}\n`;
}
if (node.nodeName == "BLOCKQUOTE") {
const body = Array.from(node.childNodes).map(node2mfm).join("").replace("\r\n", "\n").split("\n").map(line => line.match(/^\s*$/) ? line : `> ${line}`).join("\n");
return body;
}
if (node.nodeName == "#text") {
return node.data.replace("\r\n", "").replace("\n", "");
}
{
return Array.from(node.childNodes).map(node2mfm).join("");
}
});
const quote = node2mfm(div);
if (quote.trim()) {
lines = lines.concat(quote.replace(/\r\n/g, "\n").split(/\n/g));
} lines.push(text_from);
const body = encodeURIComponent(lines.join('\n'));
window.open('https://misskey.io/share?text=' + body);
})();