-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.go
executable file
·180 lines (173 loc) · 4.64 KB
/
js.go
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
package src
var (
Js = ""
JsName = "hotpic.js"
)
// 败笔
func InitJs() {
if PublishToWeb {
Js = `
/*
* hotlink.js
* 2011-08-24
*
* By Eli Grey, http://eligrey.com
* Licensed under the MIT License
* See https://github.com/eligrey/hotlink.js/blob/master/LICENSE.md
*/
/*! @source http://purl.eligrey.com/github/hotlink.js/blob/master/hotlink.js */
/*global self, document*/
var hotlink = (function(view, document) {
"use strict";
var
HTML_NS = "http://www.w3.org/1999/xhtml"
, HOTLINK_NS = "http://purl.eligrey.com/hotlink"
, doc_elt = document.documentElement
, user_agent = view.navigator.userAgent
// Some bugs just can't be solved with feature detection
, firefox = ~user_agent.indexOf("Gecko/")
, ie = ~user_agent.indexOf("MSIE")
, opera = view.opera
, click = function(elt) {
if (elt.click) {
elt.click();
} else {
var event = document.createEvent("Event");
event.initEvent("click", true, true);
elt.dispatchEvent(event);
}
}
, append = function(node, child) {
return node.appendChild(child);
}
, create_elt = function(ns, name, doc) {
return (doc || document).createElementNS(ns, name);
}
, attr = function(elt, attr, val) {
var old_val = elt.getAttribute(attr);
if (arguments.length === 3) {
if (val === false) {
elt.removeAttribute(attr);
} else {
elt.setAttribute(attr, val);
}
}
return old_val;
}
, frame_img = function(img) {
var
parent = img.parentNode
// I make the assumption that you're not using namespaces in your CSS
, container = parent.insertBefore(create_elt(HOTLINK_NS, "img"), img)
, frame = append(container, create_elt(HTML_NS, "iframe"))
, link = create_elt(HTML_NS, "a", frame.contentDocument)
, src = attr(img, "data-src", false) // img.dataset.src
, width = +attr(img, "width", false)
, height = +attr(img, "height", false)
, attrs, i, attribute
;
if(width==0){
width = +attr(img, "data-rawwidth", false);
}
if(height==0){
height = +attr(img, "data-rawheight", false);
}
if (firefox || opera) {
// Firefox & Opera will need 16px padding once they support noreferrer
// There is no other way to do this without feature detection
width += 16;
height += 16;
}
attrs = img.attributes;
i = attrs.length;
while (i--) {
// copy attributes
attribute = attrs[i];
attr(container, attribute.name, attribute.value);
attr(img, attribute.name, false);
}
parent.removeChild(img);
frame.style.verticalAlign = "bottom";
frame.style.width = width + "px";
frame.style.height = height + "px";
if(width==0){
frame.style.width = "100%";
}
//if(height==0 && height>=960){
if(height==0){
frame.style.height = "960px";
}
frame.scrolling = "no";
frame.style.border = 0;
link.rel = "noreferrer";
link.href = src;
if (firefox || ie) {
frame.src = src;
}
click(link);
}
, ready = false
, ready_queue = []
, noreferrer_supported = false
, hotlink = function(img) {
if (ready) {
if (noreferrer_supported) {
frame_img(img);
} else {
// remap data-src to src
attr(img, "src", attr(img, "data-src", false));
}
} else {
ready_queue.push(img);
}
}
, test_frame = append(doc_elt, create_elt(HTML_NS, "iframe"))
, test_link = create_elt(HTML_NS, "a", test_frame.contentDocument)
, test_link_url = "about:blank"
, on_ready = function() {
ready = true;
noreferrer_supported = test_frame.contentDocument.referrer === "";
var
i = 0
, len = ready_queue.length
;
for (; i < len; i++) {
hotlink(ready_queue[i]);
}
}
, style = create_elt(HTML_NS, "style")
, imgs = document.querySelectorAll("img[data-src]")
, i = imgs.length
, img
;
while (i--) {
img = imgs[i];
if (img.namespaceURI === HTML_NS) {
hotlink(img);
}
}
// I'm not using CSSOM insertRule becuase WebKit & Opera don't support @declarations
append(style, document.createTextNode(
"@namespace'" + HOTLINK_NS + "';img{display:block;vertical-align:bottom}"
));
append(doc_elt, style);
test_frame.style.visibility = "hidden";
test_frame.style.height = test_frame.style.border = 0;
test_link.rel = "noreferrer";
// Firefox & IE have a problem with setting document.referrer for about:blank, so
// I use the /robots.txt instead.
if (firefox || ie) {
test_link_url = "/robots.txt";
}
test_link.href = test_link_url;
test_frame.addEventListener("load", on_ready, false);
// Firefox & IE are buggy with link.click() so you need to set the src manually
if (firefox || ie) {
test_frame.src = test_link.href;
}
click(test_link);
return hotlink;
}(self, document));
`
}
}