Files
self-scripts/mesay.js
2024-08-16 16:51:52 +08:00

117 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ==UserScript==
// @name Me说
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Me说小尾巴
// @match https://linux.do/*
// @icon https://cdn.linux.do/uploads/default/original/3X/9/d/9dd49731091ce8656e94433a26a3ef36062b3994.png
// @grant none
// ==/UserScript==
(function () {
'use strict';
const emotionalState = [
'容光焕发', '无精打采', '精神抖擞',
'奄奄一息', '气宇轩昂', '疲惫不堪',
'落落大方', '愁眉苦脸', '满面春风',
'心力交瘁', '风度翩翩', '萎靡不振',
'温文尔雅', '怏怏不乐', '精神焕发',
'人见人爱,花见花看,车见车爆胎',
];
function randInt(start, end) {
return Math.floor(Math.random() * (end - start + 1)) + start;
};
function getEmotionalState() {
return emotionalState[randInt(0, emotionalState.length - 1)];
}
// 获取预加载数据
function getPreloadedData() {
const preloadedDataElement = document.querySelector("#data-preloaded");
if (!preloadedDataElement) {
throw new Error("Preloaded data element not found");
}
const preloadedData = preloadedDataElement.getAttribute("data-preloaded");
return JSON.parse(preloadedData);
};
// 获取用户名
function getUsername() {
const preloadedData = getPreloadedData();
const preloadedCurrentUserData = JSON.parse(preloadedData.currentUser);
return preloadedCurrentUserData.username;
}
// 获取话题所在分类
function getCategoryNames() {
const categories = [];
// 第一种方式
const titleWrapper = document.getElementsByClassName('title-wrapper')[0];
if (titleWrapper && titleWrapper.children[1]) {
for (let child of titleWrapper.children[1].children) {
if (child.children[0]) {
categories.push(child.children[0].innerText.trim());
}
}
}
// 第二种方式
const topicCategory = document.getElementsByClassName('topic-category')[0];
if (topicCategory) {
categories.push(...topicCategory.innerText.split('\n').map(cat => cat.trim()));
}
return categories;
}
function isSecretGarden() {
const categories = getCategoryNames();
return categories.includes("秘密花园");
}
function handleClick(event) {
if (event.target && event.target.closest('button.create')) {
processTextarea();
}
}
function handleKeydown(event) {
if (event.ctrlKey && event.key === 'Enter') {
processTextarea();
}
}
function processTextarea() {
// 仅后花园的帖子使用
if (!isSecretGarden()) { return; }
let textarea = document.querySelector('#reply-control textarea');
let text = textarea.value.trim();
let signature = `<strong>${getUsername()}</strong>`;
if (text.length > 0 && !text.includes(signature)) {
textarea.value = `${text}\n\n --- \n\n <div style="text-align:right" dir="auto">— 来自${getEmotionalState()}${signature}</div>`;
// 创建并触发 input 事件
const inputEvent = new Event('input', {
bubbles: true,
cancelable: true
});
// 触发事件
textarea.dispatchEvent(inputEvent);
}
}
function init() {
const container = document.getElementById("reply-control");
// 添加事件监听器
container.addEventListener('click', handleClick, true);
document.addEventListener('keydown', handleKeydown, true);
};
setTimeout(function () {
init();
}, 1000);
})();