Skip to content

Instantly share code, notes, and snippets.

@moroya
Created December 23, 2023 06:22
Show Gist options
  • Save moroya/d3f908bbb7b29469c1d1a5f57360277e to your computer and use it in GitHub Desktop.
Save moroya/d3f908bbb7b29469c1d1a5f57360277e to your computer and use it in GitHub Desktop.
ChromeのGoogle翻訳で <code> タグの語順がおかしくなるのを修正するブックマークレット
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);
}
});
@moroya
Copy link
Author

moroya commented Dec 23, 2023

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);}});})();

@moroya
Copy link
Author

moroya commented Dec 23, 2023

codeタグが文章中の中で使われている場合のみ置換を行うようにし、不必要な修正を行わないようにしている。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment