【伟哥自用】98堂摘要方便复制助手
// ==UserScript==
// @name 【伟哥自用】98堂摘要方便复制助手
// @namespace http://tampermonkey.net/
// @version 0.11
// @description try to take over the world!
// @author You
// @match https://sehuatang.org/forum.php?mod=viewthread&tid=*
// @match https://sehuatang.org/thread*
// @grant unsafeWindow
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
// View Page Suurce之后发现网页的meta标签里的description里正好有要找的信息
// https://stackoverflow.com/questions/7524585/how-do-i-get-the-information-from-a-meta-tag-with-javascript
// title按description的方式找会出错,可以document.getElementById("thread_subject").textContent;来找
let metaInfo = {
title:"title",
description:"description",
content:"content",
text:"text",
abstract:"abstract"
};
class Sehuatang{
fetchTitle(){
/*在创建新文件夹时,特殊字符/不能作为文件夹名,换成|吧&=*/
metaInfo.title = document.getElementById("thread_subject").textContent.replaceAll("/","|").replaceAll("[自整理]","").replaceAll("【自整理】","");
return metaInfo.title;
}// title按description的方式找会出错
fetchDescription(){
metaInfo.description = document.querySelector('meta[name="description"]').content.replaceAll("【","\n【");
return metaInfo.description;
}
getText(){
metaInfo.content = document.querySelector(".t_f").textContent.replaceAll("【","\n【");
//当正文有很多时,description就不全了,需要获取真正的正文
let href =document.querySelector(".xg1>a").href
metaInfo.text = metaInfo.title+"\n"+href+"\n"+metaInfo.content;
/* 多个换行符换成只留一个
https://stackoverflow.com/questions/6686433/replace-multiple-n-with-1-in-javascript#:~:text=string%20%3D%20string.,than%20just%20the%20first%20one.
*/
metaInfo.text = metaInfo.text.replace(/\n+/g, '\n');
return metaInfo.text;
}
getAbstract(){//没有乱七八糟的图片信息
let href =document.querySelector(".xg1>a").href
metaInfo.abstract = metaInfo.title+"\n"+href+"\n"+metaInfo.description;
/* 多个换行符换成只留一个
https://stackoverflow.com/questions/6686433/replace-multiple-n-with-1-in-javascript#:~:text=string%20%3D%20string.,than%20just%20the%20first%20one.
*/
metaInfo.abstract = metaInfo.abstract.replace(/\n+/g, '\n');
return metaInfo.abstract;
}
}
let sehua98tang = new Sehuatang();
let menuTitle = GM_registerMenuCommand ("【标题】:"+sehua98tang.fetchTitle(), function(){//"点我复制"
GM_setClipboard(metaInfo.title);
});
let menuDescription = GM_registerMenuCommand ("【描述】:"+sehua98tang.fetchDescription(), function(){//"点我复制"
// https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript
// replace只换一次,要全换掉要replaceAll函数
GM_setClipboard(metaInfo.description);
});
let menuText = GM_registerMenuCommand("【文本】:"+sehua98tang.getText(),function(){
GM_setClipboard(sehua98tang.getText());
});
let menuAbstract = GM_registerMenuCommand("【摘要】:"+sehua98tang.getAbstract(),function(){
GM_setClipboard(sehua98tang.getAbstract());
});
// Your code here...
})();