// ==UserScript== // @name U2实时预览BBCODE // @namespace https://u2.dmhy.org/ // @version 0.6.9 // @description 实时预览BBCODE // @author kysdm // @grant none // @match *://u2.dmhy.org/* // @exclude *://u2.dmhy.org/shoutbox.php* // @icon https://u2.dmhy.org/favicon.ico // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/localforage/1.10.0/localforage.min.js // @require https://unpkg.com/image-conversion@2.1.1/build/conversion.js // @license Apache-2.0 // @downloadURL none // ==/UserScript== /* 本脚本基于 Bamboo Green 界面风格进行修改 为什么会有近似功能的函数呢,问就是历史原因 等不能跑的时候再动祖传代码 /* /* GreasyFork 地址 https://greasyfork.org/zh-CN/scripts/426268 */ /* 更新日志 https://github.com/kysdm/u2_share/commits/main/u2share_bbcode.user.js */ /* 无法显示的 Tag Flash 有关的 Tag 我不知道的特殊操作 */ /* 与U2娘显示不同的标签 (非标准操作) U2允许未封闭标签,此脚本不接受此操作。 */ 'use strict'; // 声明全局变量 // https://api.jquery.com/jQuery.noConflict/ const jq = jQuery.noConflict(); // 网站语言 const lang = new lang_init(jq('#locale_selection').val());; // CSS jq('body').append(``); // JS jq('body').append(``); // 现存BBCODE元素 (async () => { if (jq('.bbcode').length === 0) return; // 判断页面是否存在 bbcode 输入框 new init(); const url = location.href.match(/u2\.dmhy\.org\/(upload|forums|comment|contactstaff|sendmessage|edit)\.php/i); if (url === null) return; await syncScroll('#bbcodejs_tbody', url[1], '.bbcode', '#bbcode2'); if (url[1] === 'upload') { await autoSaveUpload(); } else { await autoSaveMessage('#bbcodejs_tbody', '.bbcode', '#qr', url[1], '#compose'); } jq('.bbcode').parents("tr:eq(1)").after('' + lang['preview'] + '
' + '
' + '
' + await bbcode2html(jq('.bbcode').val()) + '
'); syncWindowChange('.bbcode', '#bbcode2'); jq('.bbcode').bind('input propertychange', async function updateValue() { let html = await bbcode2html(jq(this).val()); jq('#bbcode2').children('.child').html(html); }); jq('.codebuttons').click(async function updateValue() { let html = await bbcode2html(jq('.bbcode').val()); jq('#bbcode2').children('.child').html(html); }); jq('#compose').find("td.embedded.smile-icon a").each(function () { jq(this).attr('href', jq(this).attr('href').replace(/javascript: SmileIT\('\[(.+?)\]','[^']+?','[^']+?'\)/gis, function (s, x) { return `javascript:void('${x}');` })); }) .click(async function () { onEditorActionBox(jq(this).attr('href'), '.bbcode'); jq('#bbcode2').children('.child').html(await bbcode2html(jq('.bbcode').val())); }); if (/u2\.dmhy\.org\/upload\.php/i.test(location.href)) { // 添加中括号 function add_brackets(txt) { if (txt === '') { return ''; } else { return '[' + txt + ']'; } }; // 检查重叠的中括号 function check_title(txt) { if (/\[{2,}|\]{2,}/g.test(txt)) { return '' + txt + ''; } else { return txt; } }; var main_title = '' + lang['select_type'] + ''; function add_main_title() { var type_id = jq('#browsecat').val(); if (type_id === '0') { // console.log('请选择分类...'); main_title = '' + lang['select_type'] + ''; } else if (['9', '411', '413', '12', '13', '14', '15', '16', '17', '410', '412'].indexOf(type_id) !== -1) { // console.log('分类ID是: ' + type_id + ' anime'); main_title = '' + add_brackets(jq('#anime_chinese-input').val()) + add_brackets(jq('#anime_english-input').val()) + add_brackets(jq('#anime_original-input').val()) + add_brackets(jq('#anime_source-input').val()) + add_brackets(jq('#anime_resolution-input').val()) + add_brackets(jq('#anime_episode-input').val()) + add_brackets(jq('#anime_container-input').val()) + add_brackets(jq('#anime_extra-input').val()) + ''; } else if (['21', '22', '23'].indexOf(type_id) !== -1) { // console.log('分类ID是: ' + type_id + ' manga'); main_title = '' + add_brackets(jq('#manga_title-input').val()) + add_brackets(jq('#manga_author-input').val()) + add_brackets(jq('#manga_volume-input').val()) + add_brackets(jq('#manga_ended').find("select").val()) + add_brackets(jq('#manga_publisher-input').val()) + add_brackets(jq('#manga_remark-input').val()) + ''; } else if (type_id === '30') { // console.log('分类ID是: ' + type_id + ' music'); var prefix_1 = jq('#music_prefix').find("select").val(); var prefix_2 = jq('#music_collection').find("select").val(); if (['EAC', 'XLD'].indexOf(prefix_1) !== -1) { var music_quality = false; } else if (['Hi-Res', 'Web'].indexOf(prefix_1) !== -1) { var music_quality = true; }; switch (prefix_2) { case "0": // 单张 main_title = '' + add_brackets(prefix_1) + add_brackets(jq('#music_date-input').val()) + add_brackets(jq('#music_category-input').val()) + add_brackets(jq('#music_artist-input').val()) + add_brackets(jq('#music_title-input').val()) + add_brackets(jq('#music_serial_number-input').val()) + add_brackets((() => { if (music_quality) { return jq('#music_quality-input').val(); } else { return ''; } })()) + add_brackets(jq('#music_format-input').val()) + ''; break; case "1": // 合集 main_title = '' + add_brackets(prefix_1) + add_brackets('合集') + add_brackets(jq('#music_category-input').val()) + add_brackets(jq('#music_title-input').val()) + add_brackets(jq('#music_quantity-input').val()) + add_brackets((() => { if (music_quality) { return jq('#music_quality-input').val(); } else { return ''; } })()) + ''; break; } } else if (type_id === '40') { // console.log('分类ID是: ' + type_id + ' other'); main_title = '' + jq('#other_title-input').val() + ''; } else { // console.log('分类ID是: ' + type_id); } jq('#checktitle').html(check_title(main_title)); } jq("#browsecat").change(() => { new add_main_title; }); jq(".torrent-info-input").bind('input propertychange', () => { new add_main_title; }); jq('#other_title').after('' + lang['main_title'] + '' + '' + main_title + '' ); }; function init() { const type = 'original'; let h1 = jq('.codebuttons').eq(6).parent().html(); let h2 = jq('.codebuttons').eq(7).parent().html(); let h3 = jq('.codebuttons').eq(8).parent().html(); jq('.codebuttons').eq(8).parent().remove(); jq('.codebuttons').eq(7).parent().remove(); jq('.codebuttons').eq(6).parent().remove(); jq('input[value="URL"]').parent().remove(); jq('input[value="IMG"]').parent() .before(``) .before(``) .after(``) .after(``) .after(``) .after(``) .after(``); jq('input[value="QUOTE"]').parent() .after(``) .after(``) .after(``) .after(``) .after(``); jq('.codebuttons').parents('table').eq(0).after('
'); jq('#bbcodejs_tbody').append('
' + h1 + h2 + h3 + '
'); const margin = jq('.codebuttons').parents('tbody').eq(0).width() - jq("#bbcodejs_select").width() - 2.6; jq("#bbcodejs_select").css("margin-left", margin + "px"); jq(`[name="${type}_bbcode_button"]`).click(function () { onEditorActionBox(this.value, `.bbcode`); }); } })(); async function sleep(interval) { return new Promise(resolve => { setTimeout(resolve, interval); }) }; async function bbcode2html(bbcodestr) { var tempCode = new Array(); var tempCodeCount = 0; let lost_tags = new Array(); function addTempCode(value) { tempCode[tempCodeCount] = value; let returnstr = ""; tempCodeCount++; return returnstr; }; const escape_reg = new RegExp("[&\"\'<>]", "g"); bbcodestr = bbcodestr.replace(escape_reg, function (s, x) { switch (s) { case '&': return '&'; case '"': return '"'; case "'": return '''; case '<': return '<'; case '>': return '>'; default: return s; }; }); bbcodestr = bbcodestr.replace(/\r\n/g, () => { return '
' }); bbcodestr = bbcodestr.replace(/\n/g, () => { return '
' }); bbcodestr = bbcodestr.replace(/\r/g, () => { return '
' }); let br_end = ''; // 对结尾的换行符进行计数 if (br = bbcodestr.match(/(?:
)+$/)) { br_end = br[0]; const regex = new RegExp(`${br_end}$`, ""); bbcodestr = bbcodestr.replace(regex, ''); }; const checkLostTags = (value, r_tag_start, r_tag_end) => { let state = false; let r_tag_start_exec = r_tag_start.exec(value); let index_start = r_tag_start_exec ? (r_tag_start_exec.index + r_tag_start_exec[0].length) : 0; let r_tag_end_exec = r_tag_end.exec(value.slice(index_start)); if (r_tag_start_exec && !r_tag_end_exec) { let tag_start_val = r_tag_start_exec.groups.tag;; console.log('检测到丢失的标签 => ' + `[/${tag_start_val}]`); lost_tags.push(`[/${tag_start_val}]`) // value = value + `[/${tag_start_val}]`; state = true; }; // return { "value": value, "state": state }; return { "state": state }; }; const url = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { textarea = textarea.replace(/\[url=.(?:"){0,2}\]/i, function (s) { return '[url]'; }); } if (val) { const lost = checkLostTags(textarea, /\[(?url)=[^\[]*?/i, /\[\/(?url)\]/i); if (lost.state) { return textarea.replace(/\[url=[^\[]*?/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[url=(.+?)\](.*?)\[\/url\]/i, function (all, url, text) { if (url.match(/\s|\[/)) return addTempCode(all); let tmp = url.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!tmp.match(/"/)) url = tmp; else { if (url.match(/"/g).length === 1) url = url.replace('"', ''); } return addTempCode('' + text + ''); }); } else { const lost = checkLostTags(textarea, /\[(?url)\]/i, /\[\/(?url)\]/i); if (lost.state) { return textarea.replace(/\[url\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[url\](.+?)\[\/url\]/i, function (s, x) { if (x.match(/\s|\[/i)) return addTempCode(s); return addTempCode('' + x + ''); }); }; }; // 注释 const rt = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[rt=.*?\]/i, function (s) { return addTempCode(s); }); } else if (!val) { return textarea.replace('[rt]', function (s) { return addTempCode(s); }) } else { const lost = checkLostTags(textarea, /\[(?rt)=[^\[]*?/i, /\[\/(?rt)\]/i); if (lost.state) { return textarea.replace(/\[rt=[^\[]*?/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[rt=(.+?)\](.*?)\[\/rt\]/i, function (all, tval, text) { if (tval.match(/\[/i)) return addTempCode(all); let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!tmp.match(/"/)) tval = tmp; return addTempCode('' + text + '(' + tval + ')'); }); }; }; // 字体 const font = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[font=.*?]/i, function (s) { return addTempCode(s); }); } else if (!val) { return textarea.replace('[font]', function (s) { return addTempCode(s); }) } else { const lost = checkLostTags(textarea, /\[(?font)=[^\[]*?\]/i, /\[\/(?font)\]/i); if (lost.state) { return textarea.replace(/\[font=[^\[]*?/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[font=(.+?)\](.*?)\[\/font\]/i, function (all, tval, text) { if (tval.match(/\[/i)) return '[' + addTempCode(`font=`) + `${tval}]${text}`; let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!/"/.test(tmp)) { tval = tmp; } else { if (tval.match(/"/g).length === 1) tval = tval.replace('"', ''); }; return '' + text + ''; }); }; }; // 颜色 const color = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[color=.*?\]/i, function (s) { return addTempCode(s); }); } else if (!val) { return textarea.replace('[color]', function (s) { return addTempCode(s); }) } else { const lost = checkLostTags(textarea, /\[(?color)=[^\[]*?\]/i, /\[\/(?color)\]/i); if (lost.state) { return textarea.replace(/\[color=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[color=(.+?)\](.*?)\[\/color\]/i, function (all, tval, text) { if (tval.match(/\[/i)) return addTempCode(all);; let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!/"/.test(tmp)) { tval = tmp; } else { if (tval.match(/"/g).length === 1) tval = tval.replace('"', ''); }; return '' + text + ''; }); }; }; // 文字大小 const size = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[size=.*?\]/i, function (s) { return addTempCode(s); }); } else if (!val) { return textarea.replace('[size]', function (s) { return addTempCode(s); }) } else { const lost = checkLostTags(textarea, /\[(?size)=[^\[]*?\]/i, /\[\/(?size)\]/i); if (lost.state) { return textarea.replace(/\[size=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[size=(.+?)\](.*?)\[\/size\]/i, function (all, tval, text) { // size只允许1-9的数字 if (!tval.match(/^(?:")?[0-9](?:")?$/)) return addTempCode(all); let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!/"/.test(tmp)) { tval = tmp; } else { if (tval.match(/"/g).length === 1) tval = tval.replace('"', ''); }; return '' + text + ''; }); }; }; const pre = (val, textarea) => { if (val) { return textarea.replace(/\[pre=(.*?)\]/i, function (s, v) { return addTempCode('[pre=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?pre)\]/i, /\[\/(?pre)\]/i); if (lost.state) { return textarea.replace(/\[pre\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[pre\](.*?)\[\/pre\]/i, function (all, text) { return '
' + text + '
'; }); }; const b = (val, textarea) => { if (val) { return textarea.replace(/\[b=(.*?)\]/i, function (s, v) { return addTempCode('[b=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?b)\]/i, /\[\/(?b)\]/i); if (lost.state) { return textarea.replace(/\[b\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[b\](.*?)\[\/b\]/i, function (all, text) { return '' + text + ''; }); }; const i = (val, textarea) => { if (val) { return textarea.replace(/\[i=(.*?)\]/i, function (s, v) { return addTempCode('[i=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?i)\]/i, /\[\/(?i)\]/i); if (lost.state) { return textarea.replace(/\[i\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[i\](.*?)\[\/i\]/i, function (all, text) { return '' + text + ''; }); }; const u = (val, textarea) => { if (val) { return textarea.replace(/\[u=(.*?)\]/i, function (s, v) { return addTempCode('[u=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?u)\]/i, /\[\/(?u)\]/i); if (lost.state) { return textarea.replace(/\[u\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[u\](.*?)\[\/u\]/i, function (all, text) { return '' + text + ''; }); }; const s = (val, textarea) => { if (val) { return textarea.replace(/\[s=(.*?)\]/i, function (s, v) { return addTempCode('[s=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?s)\]/i, /\[\/(?s)\]/i); if (lost.state) { return textarea.replace(/\[s\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[s\](.*?)\[\/s\]/i, function (all, text) { return '' + text + ''; }); }; const img = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[img=.*?\]/i, function (s) { return addTempCode(s); }); } else if (val) { return textarea.replace(/\[img=(.*?)\]/i, function (all, url) { // [img=http://u2.dmhy.org/pic/logo.png] if (/^((?!"|'|>|<|;|\[|\]|#).)+\.(?:png|jpg|jpeg|gif|svg|bmp|webp)$/i.test(url)) { // url 以 .png 之类结尾 return addTempCode('image'); } else { return addTempCode(all); }; }); } else { // [img]http://u2.dmhy.org/pic/logo.png[/img] const lost = checkLostTags(textarea, /\[(?img)\]/i, /\[\/(?img)\]/i); if (lost.state) { return textarea.replace(/\[img\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[img\](.*?)\[\/img\]/i, function (all, url) { if (/^((?!"|'|>|<|;|\[|\]|#).)+\.(?:png|jpg|jpeg|gif|svg|bmp|webp)$/i.test(url)) { // url 以 .png 之类结尾 return addTempCode('image'); } else { return addTempCode(all); }; }); }; }; const imglnk = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { return textarea.replace(/\[imglnk=.*?\]/i, function (s) { return addTempCode(s); }); } else if (val) { return textarea.replace(/\[imglnk=(.*?)\]/i, function (all, url) { return addTempCode('[imglnk=') + url + ']'; }); } else { // [img]http://u2.dmhy.org/pic/logo.png[/img] const lost = checkLostTags(textarea, /\[(?imglnk)\]/i, /\[\/(?imglnk)\]/i); if (lost.state) { return textarea.replace(/\[imglnk\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[imglnk\](.*?)\[\/imglnk\]/i, function (all, url) { if (/^((?!"|'|>|<|;|\[|\]|#).)+\.(?:png|jpg|jpeg|gif|svg|bmp|webp)$/i.test(url)) { // url 以 .png 之类结尾 return addTempCode(`image`); } else { return addTempCode(all); }; }); }; }; const code = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { textarea = textarea.replace(/\[code=(?:"){0,2}/, '[code]'); }; if (val) { textarea = textarea.replace(/\[code=(.*?)\]/i, function (s, v) { return addTempCode('[code=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?code)\]/i, /\[\/(?code)\]/i); if (lost.state) { return textarea.replace(/\[code\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[code\](.*?)\[\/code\]/i, function (all, text) { return addTempCode(`
${lang['code']}
${text.replace(/(
)*$/, '')}

`); }); }; const info = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { textarea = textarea.replace(/\[info=(?:"){0,2}/, '[info]'); }; if (val) { textarea = textarea.replace(/\[info=(.*?)\]/i, function (s, v) { return addTempCode('[info=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?info)\]/i, /\[\/(?info)\]/i); if (lost.state) { return textarea.replace(/\[info\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[info\](.*?)\[\/info\]/i, function (all, text) { return addTempCode(`
${lang['info']}${text.replace(/(
)*$/, '')}
`); }); }; const mediainfo = (val, textarea) => { if (val === '=' || val === '="' || val === '=""') { textarea = textarea.replace(/\[mediainfo=(?:"){0,2}/, '[mediainfo]'); }; if (val) { textarea = textarea.replace(/\[mediainfo=(.*?)\]/i, function (s, v) { return addTempCode('[mediainfo=') + v + ']'; }); }; const lost = checkLostTags(textarea, /\[(?mediainfo)\]/i, /\[\/(?mediainfo)\]/i); if (lost.state) { return textarea.replace(/\[mediainfo\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[mediainfo\](.*?)\[\/mediainfo\]/i, function (all, text) { return addTempCode(`
${lang['mediainfo']}${text.replace(/(
)*$/, '')}
`); }); }; const quote = (val, textarea) => { if (!val) { // [quote]我爱U2分享園@動漫花園。[/quote] const lost = checkLostTags(textarea, /\[(?quote)]/i, /\[\/(?quote)\]/i); if (lost.state) { return textarea.replace(/\[quote\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[quote\](.*?)\[\/quote\]/i, function (s, x) { return '
' + lang['quote'] + '' + x.replace(/(
)*$/, '') + '
'; }); } else if (val === '=' || val === '="' || val === '=""') { // [quote=""]我爱U2分享園@動漫花園。[/quote] const lost = checkLostTags(textarea, /\[(?quote)=[^\[]*?\]/i, /\[\/(?quote)\]/i); if (lost.state) { return textarea.replace(/\[quote=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[quote=[^\[]*?\](.*?)\[\/quote\]/i, function (s, x) { return '
' + lang['quote'] + '' + x.replace(/(
)*$/, '') + '
'; }); } else { // [quote="ABC"]我爱U2分享園@動漫花園。[/quote] const lost = checkLostTags(textarea, /\[(?quote)=[^\[]*?\]/i, /\[\/(?quote)\]/i); if (lost.state) { return textarea.replace(/\[quote=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[quote=([^\[]*?)\](.*?)\[\/quote\]/i, function (all, tval, text) { if (tval.match(/\[/i)) return addTempCode(all);; let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!/"/.test(tmp)) { tval = tmp; }; return '
' + lang['quote'] + ': ' + tval + '' + text.replace(/(
)*$/, '') + '
'; }); }; }; const spoiler = (val, textarea) => { if (!val) { // [spoiler]我要剧透了![/spoiler] const lost = checkLostTags(textarea, /\[(?spoiler)]/i, /\[\/(?spoiler)\]/i); if (lost.state) { return textarea.replace(/\[spoiler\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[spoiler\](.*?)\[\/spoiler\]/i, function (s, x) { return `` + `` + `` + `
${lang['spoiler']}  ` + `
${x.replace(/(
)*$/, '')}
`; }); } else if (val === '=' || val === '="' || val === '=""') { // [spoiler=""]真的![/spoiler] const lost = checkLostTags(textarea, /\[(?spoiler)=.+?\]/i, /\[\/(?spoiler)\]/i); if (lost.state) { return textarea.replace(/\[spoiler=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[spoiler=.*?\](.*?)\[\/spoiler\]/i, function (s, x) { return `` + `` + `` + `
${lang['spoiler']}  ` + `
${x.replace(/(
)*$/, '')}
`; }); } else { // [spoiler="剧透是不可能的!"]真的![/spoiler] const lost = checkLostTags(textarea, /\[(?spoiler)=.+?\]/i, /\[\/(?spoiler)\]/i); if (lost.state) { return textarea.replace(/\[spoiler=[^\[]*?\]/i, function (s) { return addTempCode(s); }); }; return textarea.replace(/\[spoiler=(.*?)\](.*?)\[\/spoiler\]/i, function (all, tval, text) { if (tval.match(/\[/i)) return addTempCode(all);; let tmp = tval.replace(/^(?:")?(.*?)(?:")?$/, "$1"); if (!/"/.test(tmp)) tval = tmp; return `` + `` + `` + `
${tval}  ` + `
${text.replace(/(
)*$/, '')}
`; }); }; }; // 附件 const attach = async (val, textarea) => { const lost = checkLostTags(textarea, /\[(?attach)\]/i, /\[\/(?attach)\]/i); if (lost.state) { return textarea.replace(/\[attach\]/i, function (s) { return addTempCode(s); }); }; let db = localforage.createInstance({ name: "attachmap" }); return await replaceAsync(textarea, /\[attach(?=[^\]]*?)?\](?.*?)\[\/attach\]/i, async (...args) => { const { tag, hash } = args.slice(-1)[0]; if (tag) { return '[' + addTempCode(`attach`) + tag + `]${hash}[/attach]`; }; if (/
/.test(hash)) { return addTempCode(`[attach]`) + hash + addTempCode('[/attach]'); }; if (!hash) { console.log('内部为空'); return addTempCode(args[0]); }; // attach 标签内为空时 if (!/^\w{32}$/.test(hash)) { return `
附件 ${hash} 无效。
`; }; // attach 标签内hash不符合要求 return await db.getItem(hash).then(async (value) => { if (value !== null && value.attach_id) { // console.log('数据已存在'); if (value.attach_type === 'img') { if (Number.isFinite(value.attach_thumb)) { if (value.attach_thumb === 0) { // console.log('没有触发缩图'); return `${value.attach_name}` } else if (value.attach_thumb === 1) { // console.log('触发缩图'); return `${value.attach_name}` }; }; // 正常情况是不会到这一步的,就不判断缩图状态了 return `${value.attach_name}` } else if (value.attach_type === 'other') { return '
' + `other  ` + `${value.attach_name}` + '  ' + `(${value.attach_size})` + '
' } else if (value.attach_type === 'invalid') { // 会不会发生碰撞呢 xd return `
附件 ${args[1]} 无效。
`; }; } else { // console.log('数据不存在'); return await new Promise((resolve, reject) => { jq.ajax({ type: 'post', url: 'https://u2.dmhy.org/preview.php', contentType: "application/x-www-form-urlencoded", data: ({ "body": `[attach]${hash}[/attach]` }), success: async function (d) { // console.log('成功'); let htmlobj = jq.parseHTML(d); let span = jq(htmlobj).find('span'); let attach_normal = jq(span).children('bdo').children('div.attach'); // 普通附件 let attach_image = jq(span).children('bdo').children('img'); // 图片附件 if (attach_normal.length !== 0 && attach_image.length === 0) { // console.log('普通附件'); // console.log(attach_normal); let attach_info_obj = /(?