更新日志

  • 2023.09.19: 更新适配最新v0.15.0
  • 2023.09.09: 初版版本,适配v0.14.4

由于 Twitter 改版,RSSHub无法抓取,以至于之前的瞬间都无法同步过来。

故前些天也整上了Memos,真香。今天折腾一下,给Memos添上评论。

先试了Waline,但略复杂,想和博客共用一套,发现不太行,遂转向utterances评论系统。

快速配置

  • GitHub新建一个新的公开仓库。
  • 打开Memos,进入设置→系统,配置自定义CSS自定义脚本,并保存。

配置页面

自定义脚本

1
2
3
4
5
const repo='Doradx/comments';
const issueTerm='pathname';
const theme='github-light';

function addUtterances(){if(document.querySelectorAll('script[label="utterances"]').length>0){console.log('Remove Utterances.');document.querySelectorAll('script[label="utterances"]').forEach(e=>e.remove())}console.log('start to init utterances.');var js=document.createElement('script');js.src='https://utteranc.es/client.js';js.setAttribute('repo',repo);js.setAttribute('issue-term',issueTerm);js.setAttribute('theme',theme);js.setAttribute('crossorigin','anonymous');js.setAttribute('async',true);js.setAttribute('label','utterances');const main=document.querySelector('div.memo-wrapper');if(main==null||main.childElementCount==0){console.error('main is null');return false}main.appendChild(js);return true}var timer=setTimeout(checkMemosPage,50);function checkMemosPage(){if(window.location.href.indexOf('/m/')<0){addCommentBtn()}if(window.location.href.indexOf('/m/')==-1){console.log('Not memos page.');return false}if(document.querySelector('div.memo-wrapper')==null){console.log('Memos page not loaded.');return false};if(document.querySelectorAll('div.utterances').length>0){console.log('Utterances already added.');return true}console.log('Memos page loaded.');if(addUtterances()){console.log('Utterances added.')}else{console.error('Failed to add utterances.');timer=setTimeout(checkMemosPage,50)}}var observer=new MutationObserver(function(mutations){if(document.querySelectorAll('div.utterances').length>0){return}timer=setTimeout(checkMemosPage,100)});observer.observe(document.body,{childList:true,subtree:true});function addCommentBtn(){console.log('Start to add comment btn.')var memosDivList=document.querySelectorAll('div.memo-wrapper');if(memosDivList==null){return false}console.log(memosDivList);memosDivList.forEach((div)=>{if(div.querySelector(".moments-comments-button")!=null||div.children.length==0){return}const memos_id=div.classList.item(1).split('-')[1];var btn=document.createElement('div');btn.className="moments-comments-button"btn.style="align-self:end;padding-right:10px;padding-top:10px;";btn.innerHTML=`<a href="/m/${memos_id}"target="_blank">评论💬</a>`;div.appendChild(btn);console.log('add btn for '+memos_id)})}

仅需将上述脚本的repo参数改为自己的公开仓库即可!

效果

评论区效果

参考