首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docusaurus的自定义内联脚本

Docusaurus的自定义内联脚本
EN

Stack Overflow用户
提问于 2021-08-11 05:15:34
回答 1查看 215关注 0票数 1

我正在尝试将wowhead工具提示添加到docusaurus页面。

wowhead documentation建议您在<head>部分添加以下内容:

代码语言:javascript
复制
<script>const whTooltips = {colorLinks: true, iconizeLinks: true, renameLinks: true};</script>
<script src="https://wow.zamimg.com/widgets/power.js"></script>

我可以使用脚本配置选项添加https://wow.zamimg.com/widgets/power.js,它可以很好地与defer或async一起使用:

代码语言:javascript
复制
module.exports = {
  // Snipped rest of configuration
  scripts: [
    {
      src:
        'https://wow.zamimg.com/widgets/power.js',
      defer: true,
    },
  ],

对于内联部分<script>const whTooltips = {colorLinks: true, iconizeLinks: true, renameLinks: true};</script>,我尝试在我的index.js <Layout>部分中使用<Head>组件,但没有成功。

我如何正确地将这个内联脚本添加到docusaurus中,以便它在wowhead脚本之前加载?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 04:39:47

使用来自D.Kastier的建议,我成功地解决了我的问题,尽管它不是很优雅。

要正确加载脚本,并在页面初始加载后进行更新,请执行以下操作:

代码语言:javascript
复制
     injectHtmlTags() {
       return {
         headTags: [
           // https://www.wowhead.com/tooltips
           {
             tagName: 'script',
             innerHTML: `
               const whTooltips = {colorLinks: true, iconizeLinks: true, renameLinks: true};

               document.addEventListener('readystatechange', event => {
                 if (event.target.readyState === "complete") {
                   console.log('Updating tooltips from plugin');
                   window.$WowheadPower.refreshLinks();
                 }
                });
             `,
           },
           {
             tagName: 'script',
             attributes: {
               defer: true,
               src: 'https://wow.zamimg.com/widgets/power.js',
             },
           },
         ],
       };
     },

然后在每次页面更改时更新工具提示:

代码语言:javascript
复制
    onRouteUpdate({location}) {
      setTimeout(function() {           
          window.$WowheadPower.refreshLinks(); 
        }, 0);
    },
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68736346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档