Created
February 6, 2024 17:03
-
-
Save GregTheMadMonk/e646b8ba59aa8539c51c878783ed8fb5 to your computer and use it in GitHub Desktop.
Кнопка для цитирования по выделению для Nasha-Life
This file contains 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
function getPost(node) { | |
while (node.className != 'alt2' && node.className != 'alt1') node = node.parentElement; | |
return node; | |
} | |
function getCite(noauthor) { | |
let sel = window.getSelection() | |
let post = getPost(sel.anchorNode).children[0] | |
let name = ((post.children.length == 0) ? post : post.children[1]).innerText | |
let content = sel.getRangeAt(0).cloneContents() | |
let text = "" | |
let traverse = (node) => { | |
console.log(node) | |
if (!node.children || node.children.length == 0) { | |
text += node.textContent | |
return | |
} | |
Array.from(node.childNodes).forEach(child => { | |
let tag = null | |
let attrs = '' | |
let unary = false | |
switch (child.tagName) { | |
case "BR": | |
return | |
case "B": | |
tag = 'b' | |
break | |
case "I": | |
tag = 'i' | |
break | |
case "U": | |
tag = 'u' | |
break | |
case "S": | |
tag = 's' | |
break | |
case "CODE": | |
tag = 'code' | |
break | |
case "FONT": | |
if (child.getAttribute("color") != null) { | |
tag = 'color' | |
attrs = '=' + child.getAttribute("color") | |
} else if (child.getAttribute("size") != null) { | |
tag = 'size' | |
attrs = '=' + child.getAttribute("size") | |
} | |
break | |
case 'A': | |
tag = 'url' | |
attrs = '=' + child.href | |
break | |
case 'OL': | |
tag = 'list' | |
attrs = '=' + child.getAttribute("type") | |
break | |
case 'LI': | |
tag = '*' | |
unary = true | |
break | |
} | |
if (tag != null) text += '[' + tag + attrs + ']' | |
traverse(child) | |
if (!unary && tag != null) text += '[/' + tag + ']' | |
}) | |
} | |
traverse(content) | |
if (noauthor) return "[quote]" + text + "[/quote]" | |
return "[quote][i][color=orange]Оригинальное сообщение от " + name + "[/color][/i]\n\n" + text + "[/quote]" | |
} | |
function makeCite(noauthor) { | |
let text = document.querySelector("textarea#message") | |
let before = text.value.substr(0, text.selectionStart) | |
let after = text.value.substr(text.selectionEnd) | |
if (!before.endsWith('\n')) before += '\n' | |
if (!after.startsWith('\n')) after = '\n' + after | |
text.value = before + getCite(noauthor).trim() + after | |
} | |
let tr = document.createElement('tr') | |
tr.appendChild(document.createElement('td')) | |
let td = document.createElement('td') | |
let but = document.createElement('span') | |
but.innerText = 'Цитировать выделение' | |
but.setAttribute('style', 'border: 1px yellow solid; padding: .25em .5ch; margin: .5em .5ch; cursor: pointer;') | |
but.onmousedown = (e) => { if (e.button == 0) makeCite(false); } | |
let but2 = document.createElement('span') | |
but2.innerText = 'Цитировать выделение без автора' | |
but2.setAttribute('style', 'border: 1px yellow solid; padding: .25em .5ch; margin: .5em .5ch; cursor: pointer;') | |
but2.onmousedown = (e) => { if (e.button == 0) makeCite(true); } | |
td.appendChild(but) | |
td.appendChild(but2) | |
tr.appendChild(td) | |
document.querySelector('form[name=vbform] tbody').appendChild(tr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment