打开主菜单
首页
随机
最近更改
特殊页面
沙盒
互助客栈
方针与指引
帮助
页面信息
链入页面
相关更改
设置
关于LLWiki
免责声明
LLWiki
搜索
用户菜单
18.188.103.162
讨论
监视列表
贡献
登录
LLWiki正在建设中,欢迎
加入我们
!
查看“User:Bhsd/photrans.js”的源代码
←
User:Bhsd/photrans.js
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您没有权限编辑此JavaScript页面,因为此页面包含另一位用户的个人设置。
您可以查看和复制此页面的源代码。
//<nowiki> (() => { let dialog; $('.Lyrics_box').on('contextmenu', '.Lyrics_original', e => { e.preventDefault(); const selection = getSelection(), node = selection.anchorNode, offset = selection.anchorOffset, focus = selection.focusOffset; if (!node || selection.focusNode != node) { return; } switch (node.parentNode.nodeName) { case 'SPAN': case 'DIV': OO.ui.prompt('请输入注音:').then(str => { if (!str) { return; } const text = node.splitText(offset < focus ? offset : focus); text.splitText(offset < focus ? focus - offset : offset - focus); $(text).replaceWith( $('<ruby>', {html: [$('<rb>', {text: text.textContent}), $('<rt>', {text: str})]}) ); }); break; case 'RB': OO.ui.prompt('请输入注音:').then(str => { if (str) { $(node).closest( 'ruby' ).children( 'rt' ).text( str ); } }); } }).on('dblclick', '.Lyrics_original', e => { const message = $('<div>', {text: [...$(e.delegateTarget).find( '.Lyrics_original' ).map(function(i) { const node = this.cloneNode( true ); // 以下所有replaceWith均使用vanilla JS,因为jQuery需要额外转义且不会立即执行 [...node.getElementsByTagName( 'ruby' )].forEach(ele => { ele.replaceWith( `|${ ele.firstChild.textContent }_${ele.querySelector( 'rt' ).textContent}|` ); }); let color = false; [...node.children].forEach(ele => { let text = ele.textContent; if (text.includes( '_' )) { text = `{{photrans|${text}}}`.replace(/(\s+)/g, '}}$1{{photrans|').replaceAll('||', '|') .replaceAll('|}}', '}}').replace(/{{photrans\|([^_]*?)}}/g, '$1').replaceAll('{{photrans}}', ''); } if (ele.className) { color = true; ele.replaceWith( `@${[...ele.classList].filter(c => c.endsWith( '-lyrics' )) .map(c => c.slice(0, -7)).join() }@${text}` ); } else { ele.replaceWith( `${color ? '@@' : ''}${text}` ); color = false; } }); return node.textContent || (i === 0 ? '<nowiki/>' : ''); })].join( '\n' ), contentEditable: true}); if (!dialog) { dialog = new OO.ui.MessageDialog({id: 'photrans-dialog'}); mw.loader.addStyleTag( '#photrans-dialog .oo-ui-messageDialog-message { white-space: pre-wrap; }' ); } mw.dialog(dialog, [{label: '好的'}], message, '请复制以下代码', {size: 'large'}); }).attr('contentEditable', true); let $div; const $span = $('<span>', {contentEditable: true}), $handler = $('<div>').css({height: '1em', cursor: 'move'}); mw.photrans = (line) => { const lyrics = [...line.children].map(ele => ele.innerHTML.replace(/<ruby>(.+)<rp>\[<\/rp><rt>(.+)<\/rt><rp>]<\/rp><\/ruby>/, '|$1_$2|') ).join( '' ); if (!$div) { mw.loader.using( 'jquery.ui' ).then(() => { $div = $('<div>', {class: 'inspector-field', html: [$handler, $span.text( lyrics )]}) .appendTo( document.body ).draggable({handle: $handler}); }); } else { $span.text( lyrics ); } }; }) (); //</nowiki>
返回
User:Bhsd/photrans.js
。