-
Notifications
You must be signed in to change notification settings - Fork 2
/
nolazyload.user.js
143 lines (128 loc) · 4.4 KB
/
nolazyload.user.js
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
// ==UserScript==
// @name nolazyload
// @namespace https://greasyfork.org/zh-CN/scripts/791-nolazyload
// @version 7.0.4
// @description 将lazyload图片提前读进缓存,提高加载速度 preload all lazyload pictures at once
// @include *
// @grant none
// @icon https://async.date/icons/nolazyload.ico
// @homepageURL https://greasyfork.org/zh-CN/scripts/791-nolazyload
// @installURL https://greasyfork.org/scripts/791-nolazyload/code/nolazyload.user.js
// @updateURL https://greasyfork.org/scripts/791-nolazyload/code/nolazyload.user.js
// @copyright 反馈和建议 feedback & suggest E-mail: [email protected]
// ==/UserScript==
//图片的真实标签 pics's real tags
let nolz_lazytags = ["src9","data-url","data-ks-lazyload","data-ks-lazyload-custom","data-lazy-load-src","data-lazyload","original","file","data-src","data-cover","data-original","data-thumb","real_src","src2","data-imageurl","data-defer-src","data-placeholder","origin-src","data-actualsrc","org_src","data-lazyload-src","src1","#src"];
//已加载图片 loaded pics
let nolz_lazypics = [];
//直接加载图片,看起来会比缓存快一些 instant load pics,it will be seems faster then cache
let nolz_inspics = ["item.taobao.com","detail.tmall.com","detail.1688.com","mp.weixin.qq.com"];
//白名单 以下域名不启用脚本 white list ,at those domains ,this script wont's work
let nolz_wlist = ["search.taobao.com","list.tmall.com","s.taobao.com"];
let nolz_prehref = document.location.href;
let nolz_nolazyload = function() //主函数
{
try
{
if (document.location.href != nolz_prehref){
//与上一次URL不一至,清一下内存
nolz_prehref = document.location.href;
nolz_lazypics = null;
nolz_lazypics = [];
}
let hostn = document.location.hostname;
for(let i=0; i<nolz_wlist.length; i++)
{
if(hostn.indexOf(nolz_wlist[i])>-1) //检测白名单
{
return;
}
}
let imgs = document.images;
for(let i=0;i<imgs.length;i++) //所有图片循环
{
for(let j=0; j<nolz_lazytags.length; j++) //所有lazy标签循环
{
for(let k=0; k<imgs[i].attributes.length; k++) //图片的所有属性循环
{
if(imgs[i].attributes[k].nodeName == nolz_lazytags[j])
{
//console.log(imgs[i].attributes[k].nodeName);
//console.log(i);
if(imgs[i].attributes[k].nodeValue != imgs[i].src){
nolz_preload(imgs[i].attributes[k].nodeValue,nolz_lazytags[j], i);
}
}
}
}
}
//console.log("nolazyload times ");
}catch(e)
{
console.log("error"+e);
}
}
let nolz_preload = function(url,tag,img)
{
let loaded = false;
for(let i = 0; i<nolz_lazypics.length;i++) //检查是否加载过
{
if(url == nolz_lazypics[i])
{
loaded = true;
break;
}
}
if(loaded)
{
return;
}
else
{
nolz_loading(url,tag,img);
console.log("preload "+url);
}
}
let nolz_loading = function(url,tag,img)
{
if(nolz_di_check())
{
document.images[img].src=url;
document.images[img].removeAttribute(tag);
}
else
{
//new Image().src = url;//预加载图片
//添加 img
let img = document.createElement("img");
//设置 img 属性,如 id
img.setAttribute("width", "0");
img.setAttribute("height", "0");
img.setAttribute("style", "width:0px !important;height:0px !important;");
//设置 img 图片地址
img.src = url;
document.body.appendChild(img);
}
nolz_lazypics.push(url)
}
let nolz_di_check = function()
{
let hn = document.location.hostname;
for(let i=0;i<nolz_inspics.length;i++)
{
if(hn.indexOf(nolz_inspics[i])>-1) //检测页面hostname里是否包含直接加载域名
{
return true;
}
}
return false;
}
//监控网页变化
let nolz_observer = new MutationObserver(function(mutations)
{
nolz_nolazyload();
});
nolz_observer.observe(document.documentElement, {
childList: true,
subtree: true
});