// ==UserScript==
// @name 老男人助手
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 适用于老男人游戏论坛:https://bbs.oldmanemu.net/ 的小工具
// @author rock128
// @match https://bbs.oldmanemu.net/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @license GPL-3.0 License
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var SIGN_TITLE = "签到"
var HAS_HIDDEN_CONTENT_KEYWORD = "本帖含有隐藏内容"
var AUTO_REPLY_MSG = []
var BLACK_LIST = []
var actionArray = [
{
describe : "页面自动签到",
open : false,
matchCondition : function(){
return isMatchPageCategory("index")
},
doAction : function(){
if($("#sign_title").text() == SIGN_TITLE){
$.xpost(xn.url("my-sign"),"",function(message){});
}
},
settingPanelHtml: '签到按钮文本: '
},
{
describe : "去除灯笼",
open : false,
matchCondition : function(){
return true
},
doAction : function(){
$(".deng") && $(".deng").css("display","none")
}
},
{
describe : "屏蔽用户帖子",
open : false,
matchCondition : function(){
return isMatchPageCategory("") || isMatchPageCategory("index")
},
doAction : function(){
$(".media.thread.tap") && $(".media.thread.tap").each(function(i,item){
var id = $(item).children().eq(0).attr("href")
id = id.replace("user-","").replace(".htm","")
if(BLACK_LIST.indexOf(id)!=-1){
$(item).hide()
}
})
},
settingPanelHtml: '黑名单列表: '
},
{
describe : "自动回复隐藏帖子",
open : false,
matchCondition : function(){
return isMatchPageCategory("thread")
},
doAction : function(){
if($(".alert-warning") && $(".alert-warning").text().indexOf(HAS_HIDDEN_CONTENT_KEYWORD) != -1){
var msg = AUTO_REPLY_MSG[Math.floor(Math.random()*AUTO_REPLY_MSG.length)];
$(".message .form-control").val(msg)
$("#quick_reply_form").submit()
}
},
settingPanelHtml:'识别页面是否有隐藏内容的关键词: 自动回复消息模板: '
}
]
Function.prototype.getMultiLine = function() {
var lines = new String(this);
lines = lines.substring(lines.indexOf("/*") + 3, lines.lastIndexOf("*/"));
return lines;
}
String.prototype.format= function() {
if(arguments.length === 0) return this;
var param = arguments[0], str= this;
if(typeof(param) === 'object') {
for(var key in param)
str = str.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
return str;
} else {
for(var i = 0; i < arguments.length; i++)
str = str.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
return str;
}
}
function localStorageGetItem(key,defaultVal){
let val = localStorage.getItem(key)
return val ? val : defaultVal
}
function localStorageGetStringArray(key,defaultVal,split=","){
let value = localStorage.getItem(key)
return (value && value.length > 0) ? value.split(split) : defaultVal
}
function loadSettingFromLocalStorage(){
SIGN_TITLE = localStorageGetItem("SIGN_TITLE","签到")
HAS_HIDDEN_CONTENT_KEYWORD = localStorageGetItem("HAS_HIDDEN_CONTENT_KEYWORD","本帖含有隐藏内容")
AUTO_REPLY_MSG = localStorageGetStringArray("AUTO_REPLY_MSG",[])
BLACK_LIST = localStorageGetStringArray("BLACK_LIST",[])
let fnOpenStatus = localStorageGetStringArray("fnOpenStatus",[])
for(let i =0;i
{describe}
{settingPanelHtml}
*/
}
function updateFunctionSwitch(){
actionArray.forEach(function(e,i){
e.open = $("#fn-checkbox-"+i)[0].checked
})
}
window.pop = function pop(){
let settingData = {
SIGN_TITLE:SIGN_TITLE,
HAS_HIDDEN_CONTENT_KEYWORD:HAS_HIDDEN_CONTENT_KEYWORD,
AUTO_REPLY_MSG:AUTO_REPLY_MSG.join("\n"),
BLACK_LIST:BLACK_LIST.join("\n")
}
let html = ""
actionArray.forEach(function(e,i){
e.fnIndex = i
e.checked= e.open ? "checked='checked'" : ""
e.displayStyle = e.open ? "" : "display:none;"
e.settingPanelHtml = e.settingPanelHtml ? e.settingPanelHtml.format(settingData) : ""
html += functionListHtml.getMultiLine().format(e)
})
html = ''+ html + '
'
new $Msg({
useHTML:true,
content:html,
type:"success",
cancle:function(){},
confirm:function(){
SIGN_TITLE = $("#sign-key").val();
HAS_HIDDEN_CONTENT_KEYWORD = $("#hide-key").val();
AUTO_REPLY_MSG = $("#auto-reply").val().split("\n");
BLACK_LIST = $("#black-list").val().split("\n");
updateFunctionSwitch()
saveSettingFromLocalStorage()
window.location.reload();
new $Msg({content:"设置已经保存"})
}
})
}
// 设置按钮代码参考:https://greasyfork.org/zh-CN/scripts/419215-%E8%87%AA%E5%8A%A8%E6%97%A0%E7%BC%9D%E7%BF%BB%E9%A1%B5
let _style = ``
let _html = `设置
`
document.documentElement.insertAdjacentHTML('beforeend', _style + _html);
function isMatchPageCategory(pagePrefix){
return window.location.href.startsWith(window.location.protocol + "//" + window.location.host + "/" + pagePrefix)
}
actionArray.forEach(function(e){
e.open && e.matchCondition() && e.doAction()
})
})();
// 弹窗代码地址:https://www.jb51.net/article/145251.htm
(function (window, document) {
//定义一个构造函数Msg 作为弹窗实例的构造函数。
let Msg = function (options) {
//执行初始化操作
this._init(options);
}
//定义初始化方法 并对方法传递的参数进行初始化
Msg.prototype = {
_init({
content = "", //文本内容
type = "info", //信息类型
useHTML = false, //是否解析html字符串
showIcon = false, //是否展示弹窗图标
confirm = null, //确认后得回调
cancle = null, //取消后得回调
footer = true, //是否显示底部的确认按钮
header = true, //是否显示头部信息及关闭按钮
title = "提示", //弹窗标题
contentStyle = {}, //内容样式
contentFontSize = "1.5em", //内容字体大小
btnName = ["确定", "取消"] //按钮文字内容
}) {
//将传入的值绑定到this上
this.content = content;
this.type = type;
this.useHTML = useHTML;
this.showIcon = showIcon;
this.confirm = confirm;
this.cancle = cancle;
this.footer = footer;
this.header = header;
this.title = title;
this.contentStyle = contentStyle;
this.contentFontSize = contentFontSize;
this.btnName = btnName;
//执行创建元素方法
this._creatElement();
//显示弹窗及遮罩
this._show({
el: this._el,
overlay: this._overlay
});
//绑定事件处理函数
this._bind({
el: this._el,
overlay: this._overlay
});
},
//创建弹窗元素方法
_creatElement() {
//创建最外层得包裹元素
let wrap = document.createElement("div");
wrap.className = "msg__wrap";
//定义弹窗得两个按钮
const [confirmBtnName, cancelBtnName] = this.btnName;
//判断是否显示弹窗标题
const headerHTML = this.header ?
`` : "";
//判断是否显示图标
const iconHTML = this.showIcon ?
`` : "";
//判断是否显示弹窗底部按钮
const footerHTML = this.footer ?
`` : "";
//拼接完整html
const innerHTML = `${headerHTML}
${footerHTML}`;
//将拼接的html赋值到wrap中
wrap.innerHTML = innerHTML;
//把自定义的样式进行合并
const contentStyle = {
fontSize: this.contentFontSize,
...this.contentStyle
}
//获取内容所属DOM
let content = wrap.querySelector(".msg-body .msg-body-content");
//将传过来的样式添加到contentDOM
for (const key in contentStyle) {
if (contentStyle.hasOwnProperty(key)) {
content.style[key] = contentStyle[key];
}
}
//给弹窗的conntent赋值
if (this.useHTML) {
content.innerHTML = this.content;
} else {
content.innerText = this.content;
}
//创建遮罩层
let overlay = document.createElement("div");
overlay.className = "msg__overlay";
//把dom挂载到当前实例上
this._overlay = overlay;
this._el = wrap;
},
//弹窗展现方法
_show({
el,
overlay
}) {
//把弹窗的dom和遮罩插入到页面中
document.body.appendChild(el);
document.body.appendChild(overlay);
//将弹窗显示出来 timeout进行异步处理显示动画
setTimeout(() => {
el.style.transform = "translate(-50%,-50%) scale(1,1)";
overlay.style.opacity = "1";
})
},
//关闭弹窗方法
_close({
el,
overlay
}) {
//隐藏dom
el.style.transform = "translate(-50%,-50%) scale(0,0)";
overlay.style.opcity = "0";
//根据动画时间 动画完成再移除
setTimeout(() => {
//把弹窗的dom和遮罩移除
document.body.removeChild(el)
document.body.removeChild(overlay);
}, 300);
},
//事件处理函数,为DOM绑定事件
_bind({
el,
overlay
}) {
//保存当前this
//const _this = this;
const cancle = (e) => {
this.cancle && this.cancle.call(this, e);
//隐藏弹窗
//hideMsg();
this._close({
el,
overlay
});
}
//确认弹窗
const confirm = (e) => {
this.confirm && this.confirm.call(this, e);
this._close({
el,
overlay
});
}
//顶部关闭按钮绑定事件
if (this.header) {
el.querySelector(".msg-header-close-button").addEventListener("click", cancle);
}
//弹窗底部两个按钮事件监听
if (this.footer) {
el.querySelector(".msg-footer-cancel-button").addEventListener("click", cancle);
el.querySelector(".msg-footer-confirm-button").addEventListener("click", confirm)
}
}
}
//将构造函数暴露到window,这样才能在全局作用域下直接调用
window.$Msg = Msg;
})(window, document);