mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
Fix translator not observing text change
This commit is contained in:
parent
362e97433c
commit
7bba931d2d
2
min/i18n.min.css
vendored
2
min/i18n.min.css
vendored
@ -1 +1 @@
|
||||
.textarea-container .comm-tool .comm-emoji,.textarea-container .comm-tool .comm-submit,.up-info .up-focus-btn{width:auto!important;padding:0 4px!important}.bb-comment .comment-send .comment-emoji{width:max-content!important;padding:0 4px!important}.primary-menu .nav-menu .nav-name{white-space:pre!important}.dynamic-m .dyn_menu .line{display:none!important}.tag-panel .more-button{width:44px!important}.link-navbar .main-ctnr .nav-logo{width:70px!important}.bili-header-m .profile-m .grade .bar{width:160px!important}.link-navbar .main-ctnr{width:90%!important}.n .n-data{width:auto!important;padding:10px 8px!important}.user-panel-ctnr .user-panel .content-ctnr .ctrl-btn:nth-child(2n){padding-left:8px!important}
|
||||
.textarea-container .comm-tool .comm-emoji,.textarea-container .comm-tool .comm-submit,.up-info .up-focus-btn{width:auto!important;padding:0 4px!important}.bb-comment .comment-send .comment-emoji{width:max-content!important;padding:0 4px!important}.primary-menu .nav-menu .nav-name{white-space:pre!important}.dynamic-m .dyn_menu .line,.tab-bar .line{display:none!important}.tab-bar .tab .tab-text.selected{color:var(--theme-color)!important;border-bottom:2px solid!important;box-sizing:border-box!important}.live-panel .more-button,.tag-panel .more-button{width:44px!important}.live-panel .more-button{transform:translateX(-9px)!important}.link-navbar .main-ctnr .nav-logo{width:70px!important}.bili-header-m .profile-m .grade .bar{width:160px!important}.link-navbar .main-ctnr{width:90%!important}.n .n-data{width:auto!important;padding:10px 8px!important}.user-panel-ctnr .user-panel .content-ctnr .ctrl-btn:nth-child(2n){padding-left:8px!important}
|
||||
2
min/i18n.min.js
vendored
2
min/i18n.min.js
vendored
@ -1 +1 @@
|
||||
(()=>{return(e,t)=>{const s={"日本語":"ja-JP",English:"en-US",Deutsch:"de-DE"};class a{accepts(e){return e.nodeType===Node.ELEMENT_NODE}getValue(e){return e.nodeValue}setValue(e,t){e.nodeValue=t}getElement(e){return e}translate(e){const t=this.getValue(e);if(!t||typeof t!=="string"||t==="*"){return}const s=a.map.get(t.trim());if(s===undefined){return}if(typeof s==="string"){this.setValue(e,s)}else if(Array.isArray(s)){let t=null;for(const a of s){if(typeof a==="string"){t=a}else{const{text:s,selector:r,not:l}=a;if(this.getElement(e).matches(r)!==Boolean(l)){t=s}}}if(t!==null){this.setValue(e,t)}}else{const{text:t,selector:a,not:r}=s;if(this.getElement(e).matches(a)!==Boolean(r)){this.setValue(e,t)}}}static walk(e,t){const s=document.createTreeWalker(e,NodeFilter.SHOW_TEXT|NodeFilter.SHOW_ELEMENT,null,false);let a=s.nextNode();while(a){t(a);a=s.nextNode()}}static translate(e){if(e.nodeType===Node.TEXT_NODE){a.textNode.translate(e);return}a.walk(e,e=>{for(const t of a.allTranslators){if(t.accepts(e)){t.translate(e)}}})}static translateCssMatches(){const e=a.map.get("*");if(!e){return}for(const{selector:t,text:s}of e){const e=document.querySelector(t);if(e){[...e.childNodes].filter(e=>e.nodeType===Node.TEXT_NODE).forEach(e=>e.nodeValue=s)}}}}class r extends a{accepts(e){return e.nodeType===Node.TEXT_NODE}getElement(e){return e.parentElement}}class l extends a{getValue(e){return e.getAttribute("title")}setValue(e,t){e.setAttribute("title",t)}}class n extends a{getValue(e){return e.getAttribute("placeholder")}setValue(e,t){e.setAttribute("placeholder",t)}}a.textNode=new r;a.title=new l;a.placeholder=new n;a.allTranslators=[a.textNode,a.title,a.placeholder];(async()=>{const{map:r}=await t.importAsync(`i18n.${s[e.i18nLanguage]}`);a.map=r;a.translate(document.body);a.translateCssMatches();Observer.childListSubtree("body",e=>{e.forEach(e=>{if(e.addedNodes.length>0){a.translateCssMatches()}e.addedNodes.forEach(e=>{a.translate(e)})})})})();return{export:{Translator:a,TextNodeTranslator:r,TitleTranslator:l,PlaceholderTranslator:n}}}})();
|
||||
(()=>{return(e,t)=>{const a={"日本語":"ja-JP",English:"en-US",Deutsch:"de-DE"};class r{accepts(e){return e.nodeType===Node.ELEMENT_NODE}getValue(e){return e.nodeValue}setValue(e,t){e.nodeValue=t}getElement(e){return e}translate(e){const t=this.getValue(e);if(!t||typeof t!=="string"||t==="*"){return}const a=r.map.get(t.trim());if(a===undefined){return}if(typeof a==="string"){this.setValue(e,a)}else if(Array.isArray(a)){let t=null;for(const r of a){if(typeof r==="string"){t=r}else{const{text:a,selector:s,not:l}=r;if(this.getElement(e).matches(s)!==Boolean(l)){t=a}}}if(t!==null){this.setValue(e,t)}}else{const{text:t,selector:r,not:s}=a;if(this.getElement(e).matches(r)!==Boolean(s)){this.setValue(e,t)}}}static walk(e,t){const a=document.createTreeWalker(e,NodeFilter.SHOW_TEXT|NodeFilter.SHOW_ELEMENT,null,false);let r=a.nextNode();while(r){t(r);r=a.nextNode()}}static translate(e){if(e.nodeType===Node.TEXT_NODE){r.textNode.translate(e);return}r.walk(e,e=>{for(const t of r.allTranslators){if(t.accepts(e)){t.translate(e)}}})}static translateCssMatches(){const e=r.map.get("*");if(!e){return}for(const{selector:t,text:a}of e){const e=document.querySelector(t);if(e){[...e.childNodes].filter(e=>e.nodeType===Node.TEXT_NODE).forEach(e=>e.nodeValue=a)}}}}class s extends r{accepts(e){return e.nodeType===Node.TEXT_NODE}getElement(e){return e.parentElement}}class l extends r{getValue(e){return e.getAttribute("title")}setValue(e,t){e.setAttribute("title",t)}}class n extends r{getValue(e){return e.getAttribute("placeholder")}setValue(e,t){e.setAttribute("placeholder",t)}}r.textNode=new s;r.title=new l;r.placeholder=new n;r.allTranslators=[r.textNode,r.title,r.placeholder];(async()=>{const{map:s}=await t.importAsync(`i18n.${a[e.i18nLanguage]}`);r.map=s;r.translate(document.body);r.translateCssMatches();Observer.observe("body",e=>{e.forEach(e=>{if(e.type==="childList"){if(e.addedNodes.length>0){r.translateCssMatches()}e.addedNodes.forEach(e=>{r.translate(e)})}else if(e.type==="characterData"){r.textNode.translate(e.target)}})},{characterData:true,childList:true,subtree:true})})();return{export:{Translator:r,TextNodeTranslator:s,TitleTranslator:l,PlaceholderTranslator:n}}}})();
|
||||
@ -107,16 +107,21 @@ Translator.allTranslators = [Translator.textNode, Translator.title, Translator.p
|
||||
Translator.map = map;
|
||||
Translator.translate(document.body);
|
||||
Translator.translateCssMatches();
|
||||
Observer.childListSubtree("body", records => {
|
||||
Observer.observe("body", records => {
|
||||
records.forEach(it => {
|
||||
if (it.addedNodes.length > 0) {
|
||||
Translator.translateCssMatches();
|
||||
if (it.type === "childList") {
|
||||
if (it.addedNodes.length > 0) {
|
||||
Translator.translateCssMatches();
|
||||
}
|
||||
it.addedNodes.forEach(node => {
|
||||
Translator.translate(node);
|
||||
});
|
||||
}
|
||||
else if (it.type === "characterData") {
|
||||
Translator.textNode.translate(it.target);
|
||||
}
|
||||
it.addedNodes.forEach(node => {
|
||||
Translator.translate(node);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, { characterData: true, childList: true, subtree: true });
|
||||
})();
|
||||
export default {
|
||||
export: {
|
||||
|
||||
@ -147,20 +147,27 @@ Translator.allTranslators = [Translator.textNode, Translator.title, Translator.p
|
||||
|
||||
Translator.translate(document.body);
|
||||
Translator.translateCssMatches();
|
||||
Observer.childListSubtree("body", records =>
|
||||
Observer.observe("body", records =>
|
||||
{
|
||||
records.forEach(it =>
|
||||
{
|
||||
if (it.addedNodes.length > 0)
|
||||
if (it.type === "childList")
|
||||
{
|
||||
Translator.translateCssMatches();
|
||||
if (it.addedNodes.length > 0)
|
||||
{
|
||||
Translator.translateCssMatches();
|
||||
}
|
||||
it.addedNodes.forEach(node =>
|
||||
{
|
||||
Translator.translate(node);
|
||||
});
|
||||
}
|
||||
it.addedNodes.forEach(node =>
|
||||
else if (it.type === "characterData")
|
||||
{
|
||||
Translator.translate(node);
|
||||
});
|
||||
Translator.textNode.translate(it.target);
|
||||
}
|
||||
});
|
||||
});
|
||||
}, { characterData: true, childList: true, subtree: true });
|
||||
})();
|
||||
|
||||
export default {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user