Created
December 23, 2023 06:22
-
-
Save moroya/d3f908bbb7b29469c1d1a5f57360277e to your computer and use it in GitHub Desktop.
ChromeのGoogle翻訳で <code> タグの語順がおかしくなるのを修正するブックマークレット
This file contains hidden or 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 getParentBlock(el) { | |
for (let p = el; p; p = p.parentNode) { | |
const style = window.getComputedStyle(p, null); | |
if (style && style.getPropertyValue("display") !== "inline") { | |
return p; | |
} | |
} | |
return null; | |
} | |
document.querySelectorAll("code").forEach((el) => { | |
const parentEl = getParentBlock(el); | |
if ( | |
!/\n/.test(el.innerText) && | |
parentEl && | |
parentEl.innerText !== el.innerText | |
) { | |
const span = document.createElement("span"); | |
while (el.firstChild) { | |
span.appendChild(el.firstChild); | |
} | |
for (let index = el.attributes.length - 1; index >= 0; --index) { | |
span.attributes.setNamedItem(el.attributes[index].cloneNode()); | |
} | |
span.innerText = ` \`${span.innerText}\` `; | |
el.parentNode.replaceChild(span, el); | |
} | |
}); |
codeタグが文章中の中で使われている場合のみ置換を行うようにし、不必要な修正を行わないようにしている。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bookmarklet
javascript:(()=>{function getParentBlock(el){for(let p=el;p;p=p.parentNode){const style=window.getComputedStyle(p,null);if(style&&style.getPropertyValue("display")!=="inline"){return p;}}return null;}document.querySelectorAll("code").forEach((el)=>{const parentEl=getParentBlock(el);if(!/\n/.test(el.innerText)&&parentEl&&parentEl.innerText!==el.innerText){const span=document.createElement("span");while(el.firstChild){span.appendChild(el.firstChild);}for(let index=el.attributes.length-1;index>=0;--index){span.attributes.setNamedItem(el.attributes[index].cloneNode());}span.innerText=`\`${span.innerText}\``;el.parentNode.replaceChild(span,el);}});})();