# 如何写一个通用的.js替换GoldenDict查词结果中特定字符（串）？

**URL:** <https://forum.freemdict.com/t/topic/28638>\
**Category:** 词典装饰排版交流分享\
**Created:** [2024 年5 月 7 日 07:58 UTC](https://forum.freemdict.com/t/topic/28638 "2024-05-07T07:58:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

作者： ![medp7060](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@medp7060](https://forum.freemdict.com/u/medp7060)\
发布日期： [2024 年5 月 7 日 07:58 UTC](https://forum.freemdict.com/t/topic/28638/1 "2024-05-07T07:58:54Z")

</div>

**场景** ：使用GoldenDict查词时，遇到一些小问题，例如：apostrophe符号不统一 （can’t，person’s）或者某些从“出版社”遗留下来的英文拼写错误，或者汉字用字错误，等等，又 **不能** 或者 **不想要** 修改.mdx文件。

**解决办法** ：这个可以通过.js替换GoldenDict查词结果中特定的字符（串）。这里有个例子：[OALD10 中文翻译修正 (翻译问题反馈) - #91，来自 leon406](https://forum.freemdict.com/t/topic/25852/91)

现在请高手看能不能有个 **通用的.js** 可以从一个简单的文本文件txt中读取需要 **被替换的字符（串）** 和 要替换成的 **新字符（串）** ，从而获得完美的查词结果。

---

<div class="post-metadata">

作者： ![last\_idol](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/last_idol/32/2205_2.png) [@last\_idol](https://forum.freemdict.com/u/last_idol)\
发布日期： [2024 年5 月 7 日 10:11 UTC](https://forum.freemdict.com/t/topic/28638/2 "2024-05-07T10:11:31Z")

</div>

没什么用，影响使用体验。词典结构复杂，替换很可能出现错漏，而且这个过程发生在所有词典加载完成之后，词典越多就越卡，论坛里有词典就这么干的，词典多了能卡的词典软件无响应。

---

<div class="post-metadata">

作者： ![medp7060](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@medp7060](https://forum.freemdict.com/u/medp7060)\
发布日期： [2024 年5 月 9 日 07:41 UTC](https://forum.freemdict.com/t/topic/28638/3 "2024-05-09T07:41:55Z")

</div>

有道理，不过如果只是进行简单的替换还是可以的。

请教一下，我想在一本词典的js. 和css都执行完了后，进行替换，但是下面的代码是只替换.mdx里面原来的text，而忽略所有后来js. 和css加的东西。 有什么办法获得词典最终显示的html吗？

` document.body.innerHTML`

---

<div class="post-metadata">

作者： ![last\_idol](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/last_idol/32/2205_2.png) [@last\_idol](https://forum.freemdict.com/u/last_idol)\
发布日期： [2024 年5 月 9 日 10:07 UTC](https://forum.freemdict.com/t/topic/28638/4 "2024-05-09T10:07:29Z")

</div>

不看代码不知道具体原因。如果太复杂了，也别给我看了，搞不定！只能猜测下，很多词典直接用的 `setTimeout` 加时间执行，这个时间你获取不到的，保证不了脚本已执行完。如果你 100% 确认脚本已执行完，你自己也 `setTimeout` 后在里面获取 `document.body.innerHTML`。

---

<div class="post-metadata">

作者： ![medp7060](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@medp7060](https://forum.freemdict.com/u/medp7060)\
发布日期： [2024 年5 月 9 日 14:16 UTC](https://forum.freemdict.com/t/topic/28638/5 "2024-05-09T14:16:03Z")

</div>

谢谢。这是测试的mdx. 就一个单词：replaceme

[replaceme.mdx.zip](https://forum.freemdict.com/uploads/short-url/gkWoE50qQbkO5Jj954F05EO7YIC.zip) (1.3 KB)

---

<div class="post-metadata">

作者： ![last\_idol](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/last_idol/32/2205_2.png) [@last\_idol](https://forum.freemdict.com/u/last_idol)\
发布日期： [2024 年5 月 9 日 14:21 UTC](https://forum.freemdict.com/t/topic/28638/6 "2024-05-09T14:21:24Z")

</div>

这个就是 innerHTML 的问题，内容替换不会立即执行。之前有回复过：

> [@GoldenDict-ng(Linux/macOS/Windows)基于Qt5.15.2/Qt6.X版本](https://forum.freemdict.com/t/topic/11495/1013):
>
> 可能和版本有关，看了下大辞海的javascript脚本，原版写得有点复杂，问题也有可能出在使用innerHTML替换body的时候，innerHTML的替换并不是立即执行的，而innerText是读取的已渲染的文本，在body重新渲染的间隙，此时不止是label标签，任意标签的innerText读取到的字符串有可能都是空白。 测试了下，给proc(''label.juan\_drop", …) 加…
