|
本帖最后由 阿不思的落胤 于 2024-7-22 20:33 编辑
众所周知,平时看帖的时候是可以方向键左右按来翻页的,但是, 我的收藏|收藏页 是不支持的,如果想在自己的收藏中找到自己印象中的那个主题,那就只能一次又一次点击下一页,然后往下滑,再点击下一页........
本次功能优化就是简化收藏页的翻页操作,此页面也可以用左右方向键来翻页了,并且翻页内容会直接追加到底部,页面无需刷新!!!
@Name @Name @match
油猴脚本如下(也许后续会继续优化,但是应该会新开一帖,新开的话本帖会放链接)(我真的幻想你坛要是支持markdown该多方便啊...id叫Name的和id叫Match的真的很烦!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)
(如果下面有@开头的内容被<xxx></xxx>包围了的话自己去掉吧,我懒得弄了)
- // ==UserScript==
- // @name GM论坛收藏页方向键控制上一页下一页
- // @namespace www.gamemale.com
- // @version 2024-07-20
- // @description 个人收藏界面,左方向键上一页,右方向键下一页
- // @author 阿不思的落胤
- // @match https://www.gamemale.com/home.php?mod=space&do=favorite&view=me
- // @match https://www.gamemale.com/home.php?mod=space&uid=*&do=favorite*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- var nxtButton = document.getElementsByClassName("nxt")[0];
- var prevButton = document.getElementsByClassName("prev")[0];
- //url模板,填id和page后发请求要用
- var urlPattern = 'home.php?mod=space&uid=*&do=favorite&type=all&page=*';
- //当前浏览器地址
- var curLocation = document.location;
- //当前域名
- var curOrigin = curLocation.origin;
- //当前请求参数字符串
- var curSearch = curLocation.search;
- //当前请求参数
- var curSeachParams = new URLSearchParams(curSearch);
- //获取id,此处获取方式为下一页按钮的href中获取
- var buttonHref;
- //待添加对nxtButton的判空
- if(nxtButton !== undefined){
- buttonHref = nxtButton.href;
- }else{
- buttonHref = prevButton.href;
- }
- var uid;
- var startIndex = buttonHref.indexOf('?')
- var hrefSearchParams = new URLSearchParams(buttonHref.slice(startIndex));
- uid = hrefSearchParams.get('uid');
- //获取page
- var page;
- if (curSeachParams.has('page')){
- //有参数则获取当前页
- page = curSeachParams.get('page');
- }else{
- //否则默认第1页
- page = '1';
- //跳转到有页数的url
- document.location.href = urlPattern.replace('uid=\*','uid='+uid).replace('page=\*','page=' + page);
- }
- var pageInt = parseInt(page);
- var nxtPage = pageInt + 1;
- var prevPage = pageInt - 1;
- var newUrl;
- document.addEventListener("keyup",function(e){
- if (e.key === "ArrowRight"){
- getPageData(true);
- }
- if (e.key === "ArrowLeft"){
- getPageData(false);
- }
- },false);
- //定义一个请求下一页数据的函数
- function getPageData(direction){
- //如果的前翻页
- if(!direction){
- if(prevPage < 1){
- alert("不能再往前翻了,已经到头了");
- return;
- }
- newUrl = urlPattern.replace('uid=\*','uid='+uid).replace('page=\*','page=' + prevPage--);
- }
- else{
- newUrl = urlPattern.replace('uid=\*','uid='+uid).replace('page=\*','page=' + nxtPage++);
- }
- let xhr = new XMLHttpRequest();
- xhr.open('post',curOrigin+'/'+newUrl,true);//异步
- xhr.setRequestHeader('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7');
- xhr.reponseType = 'document';
- xhr.overrideMimeType('text/html');
- xhr.addEventListener('load',function(){
- if(xhr.readyState === 4){
- if((xhr.status >= 200 && xhr.status < 300)
- || (xhr.status === 304)){
- var parser = new DOMParser();
- getAndAppendResponse(parser.parseFromString(xhr.responseText, "text/html"),this.direction);
- }
- //状态码不对的话尝试获取服务器返回的信息
- else{
- console.error(xhr.statusText);
- alert(xhr.statusText);
- }
- }
- });
- //10秒超时
- xhr.timeout = 10 * 1000;
- xhr.send();
- }
- //定义一个操作返回数据DOM的函数
- function getAndAppendResponse(XML,dirction){
- if(!XML){
- return;
- }
- //debugger;
- let responseUl = XML.getElementById('favorite_ul');
- if(!responseUl){
- alert("不能再往后翻了,已经到底了");
- return;
- }
- var FavoriteUl = document.getElementById('favorite_ul');
- let responseLis = responseUl.childNodes;
- responseLis.forEach(
- function(childNode){
- FavoriteUl.append(childNode);
- }
- );
- if (curSeachParams.has('page')){
- history.replaceState(null,'',curOrigin+'/'+newUrl);
- }else{
- //todo
- }
- }
- })();
复制代码
|
评分
-
查看全部评分
已有1人关注本帖
|