Greasy Fork is available in English.
记录已访问链接并为它们添加readed类
当前为
// ==UserScript==
// @name 头条下载图片,头条记录已访问链接并添加readed类
// @namespace http://yourwebsite.com
// @version 1.0
// @description 记录已访问链接并为它们添加readed类
// @author Your Name
// @match https://www.toutiao.com*
// @grant GM_addStyle
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 等待页面加载完毕
window.addEventListener('load', function() {
// 获取所有链接
var links = document.querySelectorAll('a.article-item');
// 从localStorage中获取已访问链接的列表
var visitedLinks = JSON.parse(localStorage.getItem('visitedLinks')) || [];
// 遍历所有链接
links.forEach(function(link) {
var href = link.href;
// 检查链接是否已经访问过
if (visitedLinks.includes(href)) {
// 添加readed类
link.classList.add('readed');
} else {
// 如果未访问过,添加点击事件处理程序来记录访问
link.addEventListener('click', function() {
visitedLinks.push(href);
localStorage.setItem('visitedLinks', JSON.stringify(visitedLinks));
});
}
});
});
// 添加自定义CSS样式
GM_addStyle('.readed { color: #999 !important; text-decoration: line-through !important; }');
// 查找包含图片的容器元素
var imageContainer = document.querySelector('.image-list'); // 请替换为实际的容器选择器
var imageContainer2 = document.querySelector('.article-content');
if (imageContainer) {
// 查找容器内的所有图片元素
var imageElements = imageContainer.querySelectorAll('img');
// 遍历图片元素并下载图片
imageElements.forEach(function(imageElement, index) {
var imgUrl = imageElement.src;
var fileName = 'image_' + index + '.jpg'; // 图片文件名,可以根据需要修改
// 使用 GM_download 下载图片
GM_download({
url: imgUrl,
name: fileName,
onerror: function(error) {
console.error('下载图片失败:', error);
},
onload: function() {
console.log('下载图片成功:', fileName);
}
});
});
}
if (imageContainer2) {
// 查找容器内的所有图片元素
var imageElements2 = imageContainer2.querySelectorAll('img');
// 遍历图片元素并下载图片
imageElements2.forEach(function(imageElement, index) {
var imgUrl = imageElement.src;
var fileName = 'image_' + index + '.jpg'; // 图片文件名,可以根据需要修改
// 使用 GM_download 下载图片
GM_download({
url: imgUrl,
name: fileName,
onerror: function(error) {
console.error('下载图片失败:', error);
},
onload: function() {
console.log('下载图片成功:', fileName);
}
});
});
}
})();