// Original work: https://jiamitongxin.dovahkiin.top/
function waibi(str) {
let len = str.length
let res = ''
const charWaibi =(c)=>{
const dic=["咕","噜","灵"]
let l = c.length
let r = ""
for(let i =0;i<l;i++){
r += dic[c[i]]
}
return r
}
for(let i=0; i<len;i++){
let tmp = str[i].charCodeAt(0).toString(3)
res += charWaibi(tmp) +"波"
}
return res
}
function biwai(str) {
const charWaibi =(c)=>{
const dic={"咕":"0","噜":"1","灵":"2"}
let l = c.length
let r = ""
for(let i =0;i<l;i++){
r += dic[c[i]] || ""
}
return r
}
let arr = str.split("波")
arr.pop()
let res = arr.reduce((a,c)=>{
if(c){
let code = parseInt(charWaibi(c)||0,3)
return a + String.fromCharCode(code)
}else{
return a
}
},'')
return res
}
function biwaiText(text) {
if (text.indexOf("波") === -1) return null
let lspace = 0
while (lspace < text.length && "咕噜灵波".indexOf(text[lspace]) === -1) lspace++
if (lspace === text.length) return null
let rspace = 0
while (rspace < text.length && "咕噜灵波".indexOf(text[text.length - 1 - rspace]) === -1) rspace++
if (text.length - lspace - rspace < 5) return null
for (let c of text.slice(lspace, text.length - rspace)) {
if ("咕噜灵波".indexOf(c) === -1) return
}
text = text.slice(0, lspace)
+ biwai(text.slice(lspace, text.length - rspace))
+ text.slice(text.length - rspace)
let tmpText = biwaiText(text)
if (tmpText) text = tmpText
return text
}
function translate(rootNode) {
for (let node of rootNode.getElementsByClassName('topic_content')) biwaiNodesRec(node)
for (let node of rootNode.getElementsByClassName('message')) biwaiNodesRec(node)
for (let node of rootNode.getElementsByClassName('cmt_sub_content')) biwaiNodesRec(node)
}
function parseTag(text, pos) {
if (text[pos] !== '[') return null
let r = pos + 1
for (; r < text.length; r++) {
if (text[r] === '[') return null
if (text[r] === ']') break
}
if (r >= text.length || text[r] !== ']') return null
const tag = text.slice(pos, r + 1)
console.log('tag:', tag)
if (tag === '[b]' ||
tag === '[/b]' ||
tag === '[i]' ||
tag === '[/i]' ||
tag === '[u]' ||
tag === '[/u]' ||
tag === '[s]' ||
tag === '[/s]' ||
tag === '[mask]' ||
tag === '[/mask]' ||
tag.startsWith('[color=') ||
tag === '[/color]' ||
tag.startsWith('[size=') ||
tag === '[/size]' ||
tag.startsWith('[url=') ||
tag === '[url]' ||
tag === '[/url]' ||
tag === '[img]' ||
tag === '[/img]') {
return tag
}
return null
}
function parseEmoticon(text, pos) {
if (text.slice(pos, pos + 4) !== '(bgm') return null
let r = pos + 4
while (r < text.length && text[r] >= '0' && text[r] <= '9') r++
if (text[r] !== ')') return null
return text.slice(pos, r + 1)
}
function parseText(text) {
let pos = 0
let result = []
let current = ''
while (pos < text.length) {
if (text[pos] === '[') {
let tag = parseTag(text, pos)
if (tag) {
if (current.length) result.push({type: 'text', content: current})
result.push({type: 'tag', content: tag})
current = ''
pos += tag.length
continue
}
}
if (text[pos] === '(') {
let emoticon = parseEmoticon(text, pos)
if (emoticon) {
if (current.length) result.push({type: 'text', content: current})
result.push({type: 'emoticon', content: emoticon})
current = ''
pos += emoticon.length
continue
}
}
current += text[pos]
pos++
}
if (current.length) result.push({type: 'text', content: current})
return result
}
function waibiText(text) {
// console.log(text)
let parsed = parseText(text)
let pos = 0
while (pos < parsed.length) {
if (parsed[pos].type === 'text') {
parsed[pos].content = waibi(parsed[pos].content)
pos++
continue
}
if (parsed[pos].type === 'emoticon') {
pos++
continue
}
if (parsed[pos].type === 'tag') {
if (parsed[pos].content === '[img]') {
pos += 2 // skip image url
continue
}
if (parsed[pos].content === '[url]') {
parsed[pos].content = '[url=' + parsed[pos + 1].content + ']' // inline url
}
pos++
continue
}
// error
break
}
// console.log(parsed)
let resultText = ''
for (let item of parsed) resultText += item.content
return resultText
}
function addWaibi(rootNode) {
for (let node of rootNode.getElementsByClassName('inputBtn')) {
if (node.type !== 'submit') continue
let form = node
while (form && form.tagName !== 'FORM') form = form.parentElement
let waibiBtn = document.createElement('input')
waibiBtn.type = 'button'
waibiBtn.className = 'inputBtn'
waibiBtn.value = '咕嚕靈波~☆( ´∀`)ノ'
waibiBtn.addEventListener('click', () => {
let textarea = form.getElementsByTagName('textarea')[0]
if (!textarea) return
textarea.value = waibiText(textarea.value)
})
node.parentElement.insertBefore(waibiBtn, node.nextSibling)
node.parentElement.insertBefore(document.createTextNode('\u00A0\u00A0'), node.nextSibling)
}
}
(function() {
'use strict';
translate(document)
addWaibi(document)
new MutationObserver((mutlist, observer) => {
for (let mut of mutlist) {
if (mut.type !== 'childList') continue
for (let node of mut.addedNodes) {
if (node.nodeType !== Node.ELEMENT_NODE) continue
translate(node)
addWaibi(node)
}
}
}).observe(document.body, {childList: true, subtree: true})
})();
http://www.keyfc.net/bbs/tools/tudoucode.aspx
https://github.com/azuse/Bgm38-in-Javascript
更新了一下地址
比歪歪吧比吧歪歪比吧卜比比比吧歪比歪吧比比卜比歪歪歪比吧歪比比吧卜比歪歪歪比比比比歪歪卜比歪歪吧比比比吧吧吧卜比歪吧吧比吧比吧吧比卜比歪歪歪比比歪歪比歪卜比歪吧歪歪歪比歪歪吧卜比歪比吧歪歪比吧比歪卜比吧比吧吧吧歪吧歪比卜比比歪吧比吧吧歪比比卜比比比比歪比歪歪比比卜比歪歪歪比吧歪比比吧卜比歪歪吧比吧吧歪歪吧卜比歪歪吧吧比吧歪歪吧歪卜比比比吧歪比歪吧比比卜比吧比吧比比比歪吧歪卜比比比吧比吧比吧歪比卜比吧比吧吧吧歪吧歪比卜比吧比比歪比歪吧吧吧卜比歪比比比歪吧比比比卜比歪歪吧吧比比吧吧比比卜