// ==UserScript==
// @name GM论坛历史回帖标记
// @namespace http://tampermonkey.net/
// @version 1.4
// @description 根据 fid 自动跳转到上次访问的 GameMale 论坛页面
// @icon https://www.gamemale.com/template/mwt2/extend/img/favicon.ico
// @author Your Name
// @match https://www.gamemale.com/forum.php*
// @match https://www.gamemale.com/forum-*-*.html
// @grant none
// ==/UserScript==
(function () {
'use strict';
// 获取当前页面的 fid
function getFidFromUrl() {
const url = window.location.href;
const match = url.match(/forum-(\d+)-\d+\.html/);
return match ? match[1] : null;
}
// 获取当前页面的 URL 参数
function getUrlParams() {
return new URLSearchParams(window.location.search);
}
// 存储 fid 和 page 到 localStorage
function storeFidAndPage(fid, page) {
if (!fid || !page) return;
try {
// 从 localStorage 中获取存储的 fid-page 映射
const fidPageMap = JSON.parse(localStorage.getItem('fidPageMap')) || {};
// 更新当前 fid 对应的 page
fidPageMap[fid] = page;
// 存储回 localStorage
localStorage.setItem('fidPageMap', JSON.stringify(fidPageMap));
console.log(`Stored fid=${fid}, page=${page}`);
} catch (error) {
console.error('Failed to store fid and page:', error);
}
}
// 根据 fid 跳转到对应的上次存储的页数
function redirectToLastPage(fid) {
if (!fid) return;
try {
// 获取上次存储的 fid-page 映射
const fidPageMap = JSON.parse(localStorage.getItem('fidPageMap')) || {};
const lastPage = fidPageMap[fid];
if (lastPage) {
// 构造跳转 URL
const redirectUrl = `https://www.gamemale.com/forum.php?mod=forumdisplay&filter=author&orderby=dateline&fid=${fid}&page=${lastPage}`;
window.location.href = redirectUrl;
console.log(`Redirecting to fid=${fid}, page=${lastPage}`);
}
} catch (error) {
console.error('Failed to redirect:', error);
}
}
// 主逻辑
function main() {
const urlParams = getUrlParams();
const currentFid = urlParams.get('fid');
const currentPage = urlParams.get('page');
// 如果是 forum.php 页面,存储当前的 fid 和 page
if (window.location.pathname === '/forum.php' && currentFid && currentPage) {
storeFidAndPage(currentFid, currentPage);
}
// 如果是 forum-*-*.html 页面,提取 fid 并跳转到上次访问的页面
if (window.location.pathname.match(/forum-\d+-\d+\.html/)) {
const fid = getFidFromUrl();
redirectToLastPage(fid);
}
}
// 执行主逻辑
main();
})();