# 修复拼写校验的Bug

# 1. 进入onlyoffice容器,备份源文件
cd /var/www/onlyoffice/documentserver/web-apps/apps/documenteditor/main 
cp  index.html  index.html_bakup 
mv ndex.html.gz_ index.html.gz_bak 
vim index.html
# 2. 修改onlyoffice服务源码

将下列代码

<script>
    window.addEventListener("message", (event) => {
     //  if (event.origin !== "http://localhost:81") return;
      if (event.data.command === "clickSpellCheck") {
        const button = document.querySelector('button[type="button"][aria-label="拼写检查"]');
        if (button) {
          button.click(); // 第一次点击
          setTimeout(() => {
            button.click(); // 第二次点击,延迟 100ms
          }, 100);
        } else {
          console.error("未找到拼写检查按钮");
        }
      }
    });
</script>

增加到下列代码前面,重启onlyoffice的docker容器

<script>
        isIEBrowser === true &&
            (document.write('<script src="../../common/main/lib/util/fix-ie-compat.js"><\/script>'),
            document.write('<script src="../../../../sdkjs/vendor/string.js"><\/script>'));
    </script>
    <script src="../../../vendor/requirejs/require.js"></script>
    <script>
        isIEBrowser === true ? require(['ie/app']) : require(['app']);
</script>
# 3. 在Vue2项目,在onlyoffice的封装组件中,加入如下方法
// 注意修改 onlyoffice服务的实际地址:`http://192.168.0.195:9999`
autoClickSpellCheck() {
  const iframe = document.getElementsByName("frameEditor")[0];
  if (iframe) {
    iframe.contentWindow.postMessage(
      { command: "clickSpellCheck" },
      "http://192.168.0.195:9999"
    );

    iframe.contentWindow.postMessage(
      { command: "clickSpellCheck" },
      "http://192.168.0.195:9999"
    );
  } else {
    console.error("未找到 iframe");
  }
},
# 4. 调用
//  消除下划线(拼写校验)主方法
const iframe = document.getElementsByName("frameEditor")[0];
if (iframe) {
	that.$refs.vabOnlyOffice.autoClickSpellCheck();  
}        
# 调用的完整代码
   // 一键替换
    changeValue() {
      const that = this;
      this.loading = true;

      let replaceStrObj = _.cloneDeep(this.formDataZhibiao);
      replaceStrObj = Object.fromEntries(
        Object.entries(replaceStrObj).map(([key, value]) => [`{${key}}`, value])
      );

      // 确保子组件已挂载
      if (
        this.$refs.vabOnlyOffice &&
        this.$refs.vabOnlyOffice.replaceTextBatch
      ) {
        this.$refs.vabOnlyOffice.onRequestRefreshFile();
        setTimeout(() => {
          console.log("【changeValue】replaceStrObj", replaceStrObj);
          this.$refs.vabOnlyOffice.replaceTextBatch(replaceStrObj);

          /*-------------  消除下划线(拼写校验) Start -------------*/
          const iframe = document.getElementsByName("frameEditor")[0];
          if (iframe) {
            that.$refs.vabOnlyOffice.autoClickSpellCheck();  
          }
          /*-------------  消除下划线(拼写校验) End -------------*/
            
          that.loading = false;
        }, 1000);
      } else {
        console.error("子组件方法不可用或未挂载");
      }
    },
# 注意,一旦发现引入其他Bug,请还原 /var/www/onlyoffice/documentserver/web-apps/apps/documenteditor/main/index.html 文件