-
Notifications
You must be signed in to change notification settings - Fork 71
/
wx_helper.js
137 lines (124 loc) · 4.62 KB
/
wx_helper.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
ENABLE_LOG = false;
function post_to_wx() {
var obj = {
'msgType': "heartbeat",
'shareTitle': document.title,
'shareURL': document.URL,
'scrollTop': document.documentElement.scrollTop,
'time': (new Date()).valueOf()
};
wx.miniProgram.postMessage({
data: obj
});
wx.miniProgram.getEnv(function (res) {
window.is_miniprogram = res.miniprogram;
});
}
setInterval(post_to_wx, 1000);
function handleOutURL(url, whitelist_flag, file_flag, file_ext) {
if (ENABLE_LOG) {
console.log("劫持链接 " + url);
}
wx.miniProgram.navigateTo({
url: '/pages/index/redirect?outURL=' + encodeURIComponent(url) +
'&inwhitelist=' + whitelist_flag +
'&handleFile=' + file_flag +
'&ext=' + file_ext,
});
}
function override_onclick(event) {
/// 优化小程序内的文件或者外链显示
if (window.is_miniprogram) {
let url = event.currentTarget.getAttribute('href');
let url_obj = new URL(url);
let whitelist = new Set();
whitelist.add("mirrors.sustech.edu.cn");
whitelist.add("bus.sustcra.com");
whitelist.add("sustech.online");
whitelist.add("daily.sustech.online");
whitelist.add("susteen.itbill.cn")
whitelist.add("");
let supportFiles = new Set();
supportFiles.add("doc");
supportFiles.add("docx");
supportFiles.add("xls");
supportFiles.add("xlsx");
supportFiles.add("ppt");
supportFiles.add("pptx");
supportFiles.add("pdf");
let the_hostname = url_obj.hostname;
let path_ext = url_obj.pathname.split('.').pop().toLowerCase();
let whitelist_flag = whitelist.has(the_hostname);
let file_flag = supportFiles.has(path_ext);
if (whitelist_flag && !file_flag) {
// 当 url 在白名单里面,且不为可微信显示的文件。
if (ENABLE_LOG) {
console.log("放行白名单页面 " + url);
}
window.location.href = url;
return;
}
event.preventDefault();
if (ENABLE_LOG) {
console.log("小程序环境,拦截外部链接或者可显示文件。");
}
handleOutURL(url, whitelist_flag, file_flag, path_ext);
}
}
function reset_all_anchor() {
var anchors = document.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if (anchor.hasAttribute("data-fancybox")) {
if (ENABLE_LOG) {
console.log("skip fancybox a tag: ", anchor.getAttribute('href'));
}
} else {
anchor.onclick = function (event) {
override_onclick(event);
}
}
}
}
setInterval(reset_all_anchor, 1000);
function isInWechatMP() {
return navigator.userAgent.match(/miniprogram/i) || window.__wxjs_environment === 'miniprogram';
};
function load_adsense() {
console.log("判断环境,加载 adsense")
if (isInWechatMP() === false) {
console.log("非小程序环境,加载");
var oScript = document.createElement("script");
oScript.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
oScript.setAttribute("async", "");
oScript.setAttribute("data-ad-client", "ca-pub-9039393129169217");
document.head.appendChild(oScript);
// <script data-ad-client="ca-pub-9039393129169217" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
} else {
console.log("小程序环境,跳过");
}
}
setTimeout("load_adsense()", 500);
// 是否启用哀悼用黑白遮罩
ENABLE_HOME_GRAY = false;
function changeGray() {
if (ENABLE_HOME_GRAY) {
if (window.location.pathname === '/') {
document.getElementsByClassName("navbar")[0].classList.add("home-gray");
document.getElementsByClassName("sidebar")[0].classList.add("home-gray");
document.getElementsByClassName("page")[0].classList.add("home-gray");
} else {
document.getElementsByClassName("page")[0].classList.remove("home-gray");
document.getElementsByClassName("sidebar")[0].classList.remove("home-gray");
document.getElementsByClassName("navbar")[0].classList.remove("home-gray");
}
}
}
domObserver = new MutationObserver(function (mutations) {
// DOM 有任何变化,包括js导致的跳转
changeGray();
});
domObserver.observe(document, { subtree: true, childList: true });
window.addEventListener('hashchange', () => {
changeGray();
}, false);