因为ar5iv是自适应系统主题的,但我希望看论文始终使用light主题而不是跟随系统设置,因此就通过JavaScript做一些后处理。之前一直用tampermonkey,中文叫篡改猴,也就是所谓的油猴插件去对网页做一些后处理。

但7月31日的时候发现不能用了,一开始还以为是ar5iv更新了,但F12找了发现原来的代码能用没问题。后来在控制台发现:Uncaught ReferenceError: MutationEvent is not defined,然后什么油猴脚本都用不了,才发现是油猴的问题。经Google查找到:Uncaught ReferenceError: MutationEvent is not defined,说是要启用chrome://flags/#mutation-events才行。但那是几个月前的问题了,应该有更加好的修复方式。

后面又看到: Throw error "Uncaught DOMException: Failed to execute 'createEvent' on 'Document': The provided event type ('MutationEvent') is invalid." in tampermonkey in content.js,解决方案是更新一下脚本到5.1.1或之后的版本。

这个倒是可以接受,但我没找到直接更新的选项,然后点了移除再装的新版。点了之后我就后悔了,果然一上去之前写的ar5iv脚本没了。但好歹是把新版装上了,而且运行脚本也确实没啥问题了。

为了防止之后继续丢失ar5iv的后处理脚本,在这里做一下简单备份。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ==UserScript==
// @name arXiv论文增强
// @namespace http://tampermonkey.net/
// @version 2024-08-06
// @description Enhance the reading experience of ar5iv papers!
// @author LiSheng2001
// @match https://ar5iv.labs.arxiv.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// ==/UserScript==
function skip_translate(elements) {
// 循环赋值"translate"="no"的属性
for (let i=0;i<elements.length;i++){
elements[i].setAttribute('translate','no')
}
}

(function() {
'use strict';
// Your code here...
// 变成亮色模式
let k = document.querySelector("html");
k.setAttribute('data-theme', 'light');

// ltx_equation为要跳过翻译的元素的特征,一般是跳过包含某个类名的元素
let equations = document.getElementsByClassName('ltx_equation')
skip_translate(equations);

// 保留引用
let bibLinks = document.querySelectorAll('a[href^="#bib"]');
skip_translate(bibLinks);

// ltx_Math是一些数学字母块
let math_blocks = document.getElementsByClassName('ltx_Math')
skip_translate(math_blocks);

// ltx_biblist是参考文献列表,不用翻译
let biblist = document.getElementsByClassName('ltx_biblist')
skip_translate(biblist);

// 禁用引用块翻译,这里通过MutationObserver监听DOM树的变化
// 创建一个 MutationObserver 实例
const observer = new MutationObserver((mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
for (let node of mutation.addedNodes) {
if (node.nodeType === 1 && node.classList.contains('ar5iv-bibitem-preview')) {
console.log('Found new element with class "ar5iv-bibitem-preview":', node);
// 在这里可以对新的元素进行处理
node.setAttribute('translate','no')
}
}
}
}
});

// 配置 MutationObserver 监听的目标和选项
const config = {
childList: true, // 监听子节点的变动
subtree: true // 监听整个子树
};

// 选择需要观察的目标节点(通常是文档的根节点)
const targetNode = document.body;

// 开始监听
observer.observe(targetNode, config);
})();

主要有以下几个增强功能: 1. 强制主题为亮色主题 2. 避开公式翻译 3. 避开参考文献翻译 4. 避开弹出的参考文献预览窗口的翻译