Skip to content

Commit

Permalink
适当优化transDesc 函数
Browse files Browse the repository at this point in the history
1. 使用`element.nextElementSibling?.id === 'translate-me'`代替`document.getElementById('translate-me')`, 减少DOM操作
2. 调整`翻译按钮`插入, 减少DOM操作
  • Loading branch information
maboloshi committed Feb 6, 2025
1 parent f12e8e0 commit 81a0f41
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,14 @@
const element = document.querySelector(selector);

// 如果元素不存在 或者 translate-me 元素已存在,那么直接返回
if (!element || document.getElementById('translate-me')) return false;
if (!element || element.nextElementSibling?.id === 'translate-me') return;

// 在元素后面插入一个翻译按钮
const buttonHTML = `<div id='translate-me' style='color: rgb(27, 149, 224); font-size: small; cursor: pointer'>翻译</div>`;
element.insertAdjacentHTML('afterend', buttonHTML);
const button = element.nextSibling;
const button = document.createElement('div');
button.id = 'translate-me';
button.style.cssText = 'color: #1b95e0; font-size: small; cursor: pointer;';
button.textContent = '翻译';
element.after(button);

// 为翻译按钮添加点击事件
button.addEventListener('click', () => {
Expand Down

0 comments on commit 81a0f41

Please sign in to comment.