Skip to content

Commit 34c6689

Browse files
committed
优化 Firefox 浏览器下部分网站无法翻页的问题(如使用 CF CDN 人机验证的网站); 优化 Firefox 浏览器下部分网站新插入内容乱码的问题(如 NGA);修复 Firefox 浏览器下自定义翻页规则编辑界面保存时没有正常显示格式错误提示的问题
1 parent bc5b947 commit 34c6689

1 file changed

Lines changed: 50 additions & 6 deletions

File tree

Autopage.user.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @name:zh-CN 自动无缝翻页
44
// @name:zh-TW 自動無縫翻頁
55
// @name:ru Автостраничник
6-
// @version 6.6.67
6+
// @version 6.6.68
77
// @author X.I.U
88
// @description ⭐Append the next page content to the bottom seamlessly (like a waterfall, Unlimited scrolling, no need to manually click on the next page) ⭐, support various forums, social networking, games, comics, novels, academics, search engines (Google, Bing, Yahoo...) and other websites~
99
// @description:zh-CN ⭐无缝加载 下一页内容 至网页底部(类似瀑布流,无限滚动,无需手动点击下一页)⭐,目前支持:【所有「Discuz!、Flarum、phpBB、MyBB、Xiuno、XenForo、NexusPHP...」论坛】【百度、谷歌(Google)、必应(Bing)、搜狗、微信、360、Yahoo、Yandex 等搜索引擎...】、贴吧、豆瓣、知乎、NGA、V2EX、起点中文、千图网、千库网、Pixabay、Pixiv、3DM、游侠网、游民星空、NexusMods、Steam 创意工坊、CS.RIN.RU、RuTracker、BT之家、萌番组、动漫花园、樱花动漫、爱恋动漫、AGE 动漫、Nyaa、SrkBT、RARBG、SubHD、423Down、不死鸟、扩展迷、小众软件、【动漫狂、动漫屋、漫画猫、漫画屋、漫画 DB、HiComic、Mangabz、Xmanhua 等漫画网站...】、PubMed、Z-Library、GreasyFork、Github、StackOverflow(以上仅一小部分常见网站,更多的写不下了...
@@ -1687,11 +1687,12 @@ function: {
16871687

16881688
// 翻页类型 1/3
16891689
function getPageE(url) {
1690-
if (!curSite.gmxhr) {
1691-
// 依靠原生 XMLHttpRequest 尝试解决因缺失跨域 cookie 导致的问题(比如一些使用 Cloudflare CDN 人机验证的网站,会出现脚本后台获取到人机验证页面)
1690+
// Chrome 浏览器可以依靠改用原生 XMLHttpRequest 尝试解决因缺失跨域 cookie 导致的问题(比如一些使用 Cloudflare CDN 人机验证的网站,会出现脚本后台获取到人机验证页面)
1691+
// Firefox 浏览器则需要使用 GM_xmlhttpRequest 的 cookiePartition 参数来解决(该参数要 Tampermonkey v5.2 及以上才有)
1692+
if (!curSite.gmxhr || !navigator.userAgent.includes('Firefox')) {
16921693
const xhr = new XMLHttpRequest();
16931694
xhr.open('GET', url, true);
1694-
xhr.overrideMimeType('text/html; charset=' + (document.characterSet||document.charset||document.inputEncoding));
1695+
//xhr.overrideMimeType('text/html; charset=' + (document.characterSet||document.charset||document.inputEncoding));
16951696

16961697
if (curSite.xRequestedWith === true) {xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest')}
16971698
//(curSite.noReferer === true) ? xhr.setRequestHeader('Referer', ''):xhr.setRequestHeader('Referer', location.href)
@@ -1720,12 +1721,15 @@ function: {
17201721
GM_xmlhttpRequest({
17211722
url: url,
17221723
method: 'GET',
1723-
overrideMimeType: 'text/html; charset=' + (document.characterSet||document.charset||document.inputEncoding),
1724+
//overrideMimeType: 'text/html; charset=' + (document.characterSet||document.charset||document.inputEncoding),
17241725
headers: {
17251726
'x-requested-with': (curSite.xRequestedWith === true) ? 'XMLHttpRequest':null,
17261727
'Referer': (curSite.noReferer === true) ? null:location.href,
17271728
'Accept': 'text/html,application/xhtml+xml,application/xml'
17281729
},
1730+
cookiePartition: { // https://github.com/Tampermonkey/tampermonkey/issues/2057
1731+
topLevelSite: location.origin
1732+
},
17291733
timeout: 5000,
17301734
onload: function (response) {
17311735
try {
@@ -2736,7 +2740,13 @@ function: {
27362740
GM_setValue('menu_customRules', customRules)
27372741
location.reload();
27382742
} catch (e) {
2739-
const match = e.message.match(/at position (\d+)/),position = parseInt(match[1]);
2743+
let match = e.message.match(/at position (\d+)/),position;
2744+
if (match) {
2745+
position = parseInt(match[1]);
2746+
} else {
2747+
match = e.message.match(/line (\d+) column (\d+)/i);
2748+
position = calculatePositionFromLineColumn(customRules,match[1],match[2])
2749+
}
27402750
console.error('自定义规则存在格式错误:\n' + e.message + '\n错误位置为该区域中间:\n------\n' + customRules.slice((position<30)?0:position-30,position+29) + '\n------\n\n常见格式错误:\n1. 逗号:每组 { } 中的最后一个值末尾不能加逗号\n2. 转义:如果正则表达式中含有转义符 \\ 那就要对其再次转义为 \\\\\n3. 引号:规则中冒号左右的内容都需要加上双引号,如果内容中含有双引号则需要对双引号转义(即 \\" 这样),或改为单引号')
27412751
window.alert('自定义规则存在格式错误:\n' + e.message + '\n错误位置为该区域中间:\n------\n' + customRules.slice((position<30)?0:position-30,position+29) + '\n------\n点击【确定】后脚本会为你定位并选中编辑框中格式错误的文本(部分格式错误定位可能不太精确,但错误一定是在选中文本的附近,如果是选中了行首的空格,则说明格式错误来自上一行末尾逗号)\n\n常见格式错误:\n1. 逗号:每组 { } 中的最后一个值末尾不能加逗号\n2. 转义:如果正则表达式中含有转义符 \\ 那就要对其再次转义为 \\\\\n3. 引号:规则中冒号左右的内容都需要加上双引号,如果内容中含有双引号则需要对双引号转义(即 \\" 这样),或改为单引号');
27422752
customRules_textarea.selectionStart = position-1; // 选中开始位置
@@ -2747,6 +2757,40 @@ function: {
27472757
getCSS('#Autopage_customRules_cancel', shadowRoot).onclick = function () {document.documentElement.style.overflow = document.body.style.overflow = ''; getCSS('#Autopage_customRules').remove();}
27482758
}
27492759

2760+
/**
2761+
* 根据行号和列号计算字符串中的 position 位置
2762+
* @param {string} text - 完整的文本内容
2763+
* @param {number} line - 行号(从1开始)
2764+
* @param {number} column - 列号(从1开始)
2765+
* @returns {number} position 位置(从0开始)
2766+
*/
2767+
function calculatePositionFromLineColumn(text, line, column) {
2768+
if (!text || line < 1 || column < 1) {
2769+
return -1;
2770+
}
2771+
2772+
const lines = text.split('\n');
2773+
2774+
// 如果指定行超过文本行数,返回-1
2775+
if (line > lines.length) {
2776+
return -1;
2777+
}
2778+
2779+
let position = 0;
2780+
2781+
// 计算前 (line-1) 行的总长度(包括换行符)
2782+
for (let i = 0; i < line - 1; i++) {
2783+
position += lines[i].length + 1; // +1 表示换行符
2784+
}
2785+
2786+
// 计算当前行的列位置(列号从1开始,position从0开始)
2787+
// 注意:列号不能超过当前行的长度+1(+1表示可以指向行尾)
2788+
const currentLine = lines[line - 1];
2789+
const columnPosition = Math.min(column - 1, currentLine.length);
2790+
2791+
return position + columnPosition;
2792+
}
2793+
27502794
// 自定义的 stringify 函数,将 [ ] 内的元素从默认的 换行显示 格式化为 一行显示,用于显示自定义翻页规则等给用户看的场景
27512795
function customStringify(obj) {
27522796
return JSON.stringify(obj, null, 4)

0 commit comments

Comments
 (0)