banner
老孙

老孙博客

资深网民孙先生
mastodon
email

Add Twikoo comments to Memos.

The code is from @Lin Mumo

Updated the script on October 23, 2023 to fit memos v0.16.1

Custom Script#

// Insert Twikoo comments on Memos v0.16.1 single page
var twikooENV = 'https://your.com/'
function addTwikooJS() { 
  var memosTwikoo = document.createElement("script");
  memosTwikoo.src = `https://cdn.staticfile.org/twikoo/1.6.22/twikoo.all.min.js`;
  var tws = document.getElementsByTagName("script")[0];
  tws.parentNode.insertBefore(memosTwikoo, tws);
};
function startTwikoo() {
  startTW = setInterval(function(){
    var nowHref = window.location.href;
    var twikooDom = document.querySelector('#twikoo') || '';
    if( nowHref.replace(/^.*\/(m)\/.*$/,'$1') == "m"){
      if(!twikooDom){
        addTwikooJS()
        setTimeout(function() {
          var memoTw = document.querySelector('.gap-2') || '';
          memoTw.insertAdjacentHTML('afterend', '<div id="mtcomment"></div>');
          twikoo.init({
            envId: twikooENV,
            el: '#mtcomment',
            path: nowHref.replace(/^.*=?(http.*\/m\/[0-9]+).*$/,'$1'),
            onCommentLoaded: function () {
              startTwikoo();
            }
          })
        }, 1500)
      }else{
        clearInterval(startTW)
      }
    }
  }, 2000)
}
startTwikoo();

Updated the script on October 17, 2023

Custom Script#

Applicable to version 0.16

//Memos v0.16 Add Twikoo Comments v2023.10.06
var twikooENV = ''  //Your https://xxxx/
function addTwikooJS() { 
  var memosTwikoo = document.createElement("script");
  memosTwikoo.src = `https://cdn.staticfile.org/twikoo/1.6.22/twikoo.all.min.js`;
  var tws = document.getElementsByTagName("script")[0];
  tws.parentNode.insertBefore(memosTwikoo, tws);
};
function startTwikoo() {
  startTW = setInterval(function(){ //Execute every 1 second
    var nowHref = window.location.href;
    var twikooDom = document.querySelector('#twikoo') || '';
    if( nowHref.replace(/^.*\/(m)\/.*$/,'$1') == "m"){//Single page
      if(!twikooDom){
        //console.log('Comments not loaded');
        addTwikooJS() //Load comments js
        setTimeout(function() { //Delay execution for 1 second
          var memoTw = document.querySelector('.resource-wrapper') || '';
          memoTw.insertAdjacentHTML('afterend', '<div id="mtcomment"></div>');
          twikoo.init({
            envId: twikooENV,
            el: '#mtcomment',
            path: nowHref.replace(/^.*=?(http.*\/m\/[0-9]+).*$/,'$1'), //v2023.08.09 Regular expression update
            onCommentLoaded: function () {
              startTwikoo()
              //console.log('Start timed execution again');
            }
          })
        }, 900)
      }else{
        //console.log('Clear timed execution');
        clearInterval(startTW)
      }
    }
  }, 2000)
}
startTwikoo();

Since the official already has a comment icon, the CSS no longer adds icons.

Updated on September 26, .memos cannot be used after upgrading to version 0.15.1

Custom Script#

// Add twikoo comments v2023.06.10
var twikooENV = 'https://twikoo.jiong.us/'
function addTwikooJS() { 
  var memosTwikoo = document.createElement("script");
  memosTwikoo.src = `https://cdn.staticfile.org/twikoo/1.6.16/twikoo.all.min.js`;
  var tws = document.getElementsByTagName("script")[0];
  tws.parentNode.insertBefore(memosTwikoo, tws);
};
function addComIcon(){
  var memoTwIcons = document.querySelectorAll('.time-text') || '';
  if(memoTwIcons){
    for(var i=0;i < memoTwIcons.length;i++){
      //if(memoTwIcon[i].hasChildNodes == false){
        memoTwIcons[i].insertAdjacentHTML('afterbegin', '<div class="twicon"><svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M896 138.667H128c-38.4 0-64 25.6-64 64v544c0 38.4 25.6 64 64 64h128v128c83.2 0 166.4-44.8 256-128h384c38.4 0 64-25.6 64-64v-544c0-38.4-25.6-64-64-64zm0 608H486.4l-19.2 19.2c-51.2 51.2-102.4 83.2-147.2 96v-115.2H128v-544h768v544z" fill="#8a8a8a"/><path d="M256 477.867a64 64 0 1 0 128 0 64 64 0 1 0-128 0zM448 477.867a64 64 0 1 0 128 0 64 64 0 1 0-128 0zM640 477.867a64 64 0 1 0 128 0 64 64 0 1 0-128 0z" fill="#8a8a8a"/></svg></div>');
      //}
    }
  }
};
function startTwikoo() {
  start = setInterval(function(){
    var twikooDom = document.getElementById('twikoo') || '';
    var memoTw = document.querySelector('.memo-wrapper') || '';
    var memoLoading = document.querySelector('.action-button-container') || '';
    var memoLoadingA = document.querySelector('.action-button-container a') || '';
    var memoTwIcons = document.querySelectorAll('.time-text .twicon') || '';
    var nowHref = window.location.href;
    if( nowHref.replace(/^.*\/(m)\/.*$/,'$1') == "m" && memoLoadingA){
      memoLoading.innerHTML = "Loading comments..."
    }
    if( nowHref.replace(/^.*\/(m)\/.*$/,'$1') == "m" && !twikooDom){
      addTwikooJS()
      if(memoTw){
        clearInterval(start)
        memoTw.insertAdjacentHTML('afterend', '<div id="mtcomment"></div>');
        setTimeout(function() {
          twikoo.init({
            envId: twikooENV,
            el: '#mtcomment',
            path: nowHref.replace(/^(.*\/m\/[0-9]+).*$/,'$1'),
            onCommentLoaded: function () {
              //console.log('Comments loaded');
              memoLoading.innerHTML = ''
              startTwikoo()
            }
          })
        }, 1000)
      }
    }
    if(nowHref.replace(/^.*\/(explore).*$/,'$1') == "explore" || nowHref.replace(/^.*\/(u).*$/,'$1') == "u"){
      memoTwIcons.forEach(memoTwIcon => {memoTwIcon.remove();});
      addComIcon()
      //console.log('Icon added successfully');
    }
    //console.log(window.location.href);
  }, 1000)
}
startTwikoo();

Custom Style#

#twikoo{padding: 1rem;background-color: rgb(63,63,70);margin: 1rem 0;border-radius: .5rem;color: #fff !important;}
.twicon{position: absolute;right: 1rem;}
.btns-container.space-x-2{margin-right:1.5rem;}
.action-button-container{color: #e5e7eb;}
.action-button-container a{display:none !important;}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.