|
|
在帖子主楼添加只看本人的按键
按shift+ctrl+c,可更换uid
- // ==UserScript==
- // @name 只看本人
- // @version 0.1
- // @description 在只看该作者旁边添加只看本人功能
- // @author MKM
- // @match https://www.gamemale.com/*
- // ==/UserScript==
- (function() {
- 'use strict';
- const STORAGE_KEY = 'discuz_my_authorid';
- function init() {
- addStyles();
- const firstPostLink = findFirstPostOnlyLink();
- if (firstPostLink) {
- addViewMeButton(firstPostLink);
- }
- }
- function findFirstPostOnlyLink() {
- const links = document.querySelectorAll('a[href*="authorid="]');
- for (let i = 0; i < links.length; i++) {
- if (links[i].textContent === '只看该作者') {
- return links[i];
- }
- }
- return null;
- }
- function addViewMeButton(onlyPostLink) {
- const viewMeLink = document.createElement('a');
- viewMeLink.href = 'javascript:;';
- viewMeLink.textContent = '只看本人';
- viewMeLink.className = 'view-me-link';
- viewMeLink.addEventListener('click', function(e) {
- e.preventDefault();
- handleViewMeClick(onlyPostLink);
- });
- const separator = document.createElement('span');
- separator.className = 'pipe';
- separator.textContent = '|';
- onlyPostLink.parentNode.insertBefore(separator, onlyPostLink.nextSibling);
- onlyPostLink.parentNode.insertBefore(viewMeLink, separator.nextSibling);
- }
- function addStyles() {
- const style = document.createElement('style');
- style.textContent = '.view-me-link{color:#369;text-decoration:none;margin:0 3px}.view-me-link:hover{text-decoration:underline;color:#f60}.authorid-input-dialog{position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:white;padding:20px;border:2px solid #369;border-radius:5px;box-shadow:0 0 10px rgba(0,0,0,0.3);z-index:99999}.authorid-input-dialog input{width:200px;padding:5px;margin:10px 0}.authorid-input-dialog button{padding:5px 15px;margin:0 5px;cursor:pointer}';
- document.head.appendChild(style);
- }
- function handleViewMeClick(onlyPostLink) {
- const savedAuthorId = localStorage.getItem(STORAGE_KEY);
- if (!savedAuthorId) {
- const currentAuthorId = extractAuthorIdFromUrl(onlyPostLink.href);
- showAuthorIdInputDialog(currentAuthorId);
- } else {
- navigateToAuthorPosts(savedAuthorId, onlyPostLink);
- }
- }
- function extractAuthorIdFromUrl(url) {
- const match = url.match(/authorid=(\d+)/);
- return match ? match[1] : null;
- }
- function showAuthorIdInputDialog(defaultAuthorId) {
- const dialog = document.createElement('div');
- dialog.className = 'authorid-input-dialog';
- dialog.innerHTML = '<p>首次使用,请输入您的UID:</p><input type="text" id="authorid-input" value="' + (defaultAuthorId || '') + '" placeholder="输入UID"><div><button id="save-authorid">保存并使用</button><button id="cancel-input">取消</button></div><p style="font-size:12px;color:#666;">空间链接内的数字即为uid,如“uid-679483”</p>';
- document.body.appendChild(dialog)
- const input = dialog.querySelector('#authorid-input');
- input.focus();
- input.select();
- dialog.querySelector('#save-authorid').addEventListener('click', function() {
- const authorId = input.value.trim();
- if (authorId && /^\d+$/.test(authorId)) {
- localStorage.setItem(STORAGE_KEY, authorId);
- document.body.removeChild(dialog);
- navigateToAuthorPosts(authorId, document.querySelector('a[href*="authorid="]'));
- } else {
- alert('请输入有效的UID');
- input.focus();
- input.select();
- }
- });
- dialog.querySelector('#cancel-input').addEventListener('click', function() {
- document.body.removeChild(dialog);
- });
- input.addEventListener('keypress', function(e) {
- if (e.key === 'Enter') {
- dialog.querySelector('#save-authorid').click();
- }
- });
- dialog.addEventListener('click', function(e) {
- if (e.target === dialog) {
- document.body.removeChild(dialog);
- }
- });
- }
- function navigateToAuthorPosts(authorId, onlyPostLink) {
- const baseUrl = onlyPostLink.href.replace(/authorid=\d+/, 'authorid=' + authorId);
- window.location.href = baseUrl;
- }
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', init);
- } else {
- init();
- }
- document.addEventListener('keydown', function(e) {
- if (e.ctrlKey && e.shiftKey && e.key === 'C') {
- localStorage.removeItem(STORAGE_KEY);
- alert('已清除保存的UID');
- }
- });
- })();
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
评分
-
查看全部评分
|