Avatar
给memos添加twikoo评论
测试
jkjoy管理员
222
2 个月前

自定义js

js 复制代码
var twikooENV = 'https://t.memos.ee/';

function addTwikooJS() {
  var memosTwikoo = document.createElement("script");
  memosTwikoo.src = `https://cdn.staticfile.org/twikoo/1.6.31/twikoo.all.min.js`;
  var tws = document.getElementsByTagName("script")[0];
  tws.parentNode.insertBefore(memosTwikoo, tws);
}

function startTwikoo() {
  window.onload = function() {
    var nowHref = window.location.href;
    var twikooDom = document.querySelector('#twikoo') || '';
    if (nowHref.replace(/^.*\/(m)\/.*$/,'$1') == "m") {
      if (!twikooDom) {
        addTwikooJS();
        var loadScriptPromise = new Promise((resolve, reject) => {
          var script = document.createElement('script');
          script.src = `https://cdn.staticfile.org/twikoo/1.6.39/twikoo.all.min.js`;
          script.onload = resolve;
          script.onerror = reject;
          document.head.appendChild(script);
        });

        loadScriptPromise.then(() => {
          return new Promise((resolve) => {
            setTimeout(resolve, 900);
          });
        }).then(() => {
          var memoTw = document.querySelector('.memo-wrapper') || '';
          memoTw.insertAdjacentHTML('afterend', '<div id="mtcomment"></div>');
          twikoo.init({
            envId: twikooENV,
            el: '#mtcomment',
            path: nowHref.replace(/^.*=?(http.*\/m\/[0-9]+).*$/,'$1'),
            onCommentLoaded: function () {
              startTwikoo();
            }
          });
        }).catch((error) => {
          console.error('Error loading Twikoo script:', error);
        });
      }
    }
  };
}

startTwikoo();