-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
180 lines (153 loc) · 6.42 KB
/
main.js
File metadata and controls
180 lines (153 loc) · 6.42 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// ==UserScript==
// @name Wallhaven 壁纸一键下载
// @name:zh-CN Wallhaven 壁纸一键下载
// @name:en WallhavenOneClickDownload
// @description 脚本支持Wallhaven在浏览模式下一键下载
// @description:zh-CN 每次保存图片需要点击图片-打开新页面-右键保存????过程太繁琐了!!!此脚本支持在浏览模式下一键下载!!!脚本基于原作者Moshel的脚本"Wallhaven壁纸网站增强"修改完成,只修改了快速下载功能.原脚本已经失效,原作者主页https://github.com/h2y/
// @description:en This script for faster download of wallpapers from the WallHaven
// @author lthero
// @namespace https://blog.lthero.cn
// @homepageURL https://blog.lthero.cn
// @supportURL https://github.com/lthero-big
// @icon https://alpha.wallhaven.cc/favicon.ico
// @license GPL-3.0
// @match https://wallhaven.cc/*
// @grant none
// @run-at document-end
// @date 28/06/2022
// @modified 24/04/2023
// @version 1.4.0.2
// ==/UserScript==
{
// 图片类 特指列表中的图片
class Pic {
constructor(elem, wallhavenScript) {
this.elem = elem;
this.wallhavenScript = wallhavenScript;
const $pic = $(elem);
this.favs = parseInt( $pic.find('.wall-favs')[0].innerHTML );
this.seen = $pic.find('figure').hasClass('thumb-seen');
this.id = $pic.find('figure').data('wallpaper-id');
//console.log("new " +this.id);
this.isPNG = ($pic.find('span.png').length > 0);
this.pre_id = this.id.toString().substr(0,2);
this.filename = `wallhaven-${this.id}`;
this.picUrl = `https://w.wallhaven.cc/full/${this.pre_id}/wallhaven-${this.id}.jpg`;
if(this.isPNG)
this.picUrl = this.picUrl.replace('.jpg', '.png');
}
desalt() {
const opacity = this.wallhavenScript.desaltPicsOpacity;
this.elem.style.opacity = opacity;
}
addDownload() {
let dlDom = $(`<a class="jsDownload" href="javascript:;" > <i class="fa fa-fw fa-cloud-download"></i></a>`)[0];
dlDom.onclick = this.download.bind(this);
$(this.elem).find('.thumb-info').append(dlDom);
//show full
let links = document.querySelectorAll(`figure.thumb-${this.id}`);
links.forEach(link => {
let previewDiv=$(`<div id="div-${this.id}" class="lazyload" sytle="display: none">`)[0];
link.style.position="relative";
$(this.elem).append(previewDiv);
link.addEventListener('mouseover', event => {
let img = document.createElement('img');
img.src = this.picUrl;
let info=$(this.elem).find('.wall-res').text().split(' x ');
//console.log(info);
img.style.maxWidth = '320px';
img.style.maxHeight = '320px';
previewDiv.style.position="absolute";
previewDiv.innerHTML = '';
previewDiv.appendChild(img);
previewDiv.style.zIndex = '9999';
//previewDiv.syyle.float="left";
previewDiv.style.display = 'block';
});
link.addEventListener('mouseout', () => {
previewDiv.style.display = 'none';
console.log("none");
});
});
}
download() {
//下载图片的方法参考:https://codeantenna.com/a/qqraSZTvUb
var x=new XMLHttpRequest();
const NfileName=this.filename;
x.open("GET", this.picUrl, true);
x.responseType = 'blob';
x.onload=function(e){
var url = window.URL.createObjectURL(x.response);
var a = document.createElement('a');
a.href = url;
a.download = `${NfileName}`;
console.log(`is ${NfileName}`);
a.click();
}
x.send();
}
}
class WallhavenScript {
constructor() {
// 显示一键下载按钮
this.ShowDownload = true;
}
workList() {
this.workListMain();
new MutationObserver( this.workListMain.bind(this) ).observe(document.body, {
attributes: false,
childList: true,
subtree: true
});
}
workListMain() {
let pics = this.getPics();
let newPics = this.filterNewPics(pics);
for(let pic of newPics) {
// 显示一键下载按钮
if(this.ShowDownload)
pic.addDownload();
}
this.pics = pics;
}
// 单图页面
workSingle() {
if(this.maxView) {
$('#header, #searchbar').hide('fast');
$('#showcase-sidebar').animate({top:0}, 'fast');
$('#main').animate({borderTopWidth:0}, 'fast');
$('#wallpaper').animate({maxWidth:'99%', maxHight:'99%'}, 'fast');
}
}
getPics() {
let elems = $('.thumb-listing-page li');
let ret = [];
for(let elem of elems)
ret.push( new Pic(elem, this) );
return ret;
}
filterNewPics(pics) {
let ret = [];
const oldElems = this.pics.map(pic=>pic.elem);
return pics.filter( pic => {
return (oldElems.indexOf(pic.elem) < 0);
});
}
/*
根据当前页面选择需要运行的功能,返回对应的 work 函数
*/
run() {
// A: 单图页面
if(location.pathname.indexOf('/w/')==0)
return this.workSingle();
this.pics = [];
return this.workList();
}
}
//new WallhavenScript().run();
const foo=new WallhavenScript();
foo.run();
window.onscroll = function () {
if ((document.documentElement.scrollHeight) == (document.documentElement.scrollTop | document.body.scrollTop) + document.documentElement.clientHeight) { console.log("down");}
}
} //end userScript