|
1 | | -if (typeof window !== 'undefined') { |
2 | | - client_id = "df82f8c4251350869fff94fb5395c580fc518956165ee82ada04308c6ff16c23"; |
3 | | - client_secret = "a12f46c9eb633ad46b1f3bf845e2c6017705b62e16258f3ca05f51e4a03a9945"; |
4 | | - const gitee_redirect_uri = location.origin; |
5 | | - |
6 | | - function redirect() { |
7 | | - const queryString = window.location.search; |
8 | | - const urlParams = new URLSearchParams(queryString); |
9 | | - const code = urlParams.get('code'); |
10 | | - const state = urlParams.get('state'); |
11 | | - if (code != null) { |
12 | | - fetch("https://gitee.com/oauth/token?grant_type=authorization_code&code=" + code + "&client_id=" + client_id + "&redirect_uri=" + gitee_redirect_uri + "&client_secret=" + client_secret, { |
13 | | - method: "POST" |
14 | | - }) |
15 | | - .then(response => response.text()) |
16 | | - .then(result => { |
17 | | - resp = JSON.parse(result) |
18 | | - console.log("result", resp); |
19 | | - if (resp.access_token != null) { |
20 | | - //获取用户信息 |
21 | | - localStorage.removeItem('gitee_user'); |
22 | | - const token = resp.access_token |
23 | | - fetch("https://gitee.com/api/v5/user?access_token=" + token, { |
24 | | - method: "GET" |
25 | | - }).then(response => response.text()) |
26 | | - .then(result => { |
27 | | - resp = JSON.parse(result) |
28 | | - console.log("result", resp); |
29 | | - localStorage.setItem('access_token', token); |
30 | | - localStorage.setItem('gitee_user', result); |
31 | | - window.location.href = state + "?timestamp=" + Date.now(); |
32 | | - }) |
33 | | - |
34 | | - } else if ("invalid_grant_accessibility" == resp.error) { |
35 | | - window.location.href = 'https://gitee.com/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + gitee_redirect_uri + '&response_type=code'; |
36 | | - } |
37 | | - }) |
38 | | - .catch(error => console.error(error)); |
39 | | - } |
| 1 | +if (typeof window !== "undefined") { |
| 2 | + client_id = "df82f8c4251350869fff94fb5395c580fc518956165ee82ada04308c6ff16c23"; |
| 3 | + client_secret = "a12f46c9eb633ad46b1f3bf845e2c6017705b62e16258f3ca05f51e4a03a9945"; |
| 4 | + const gitee_redirect_uri = location.origin; |
| 5 | + |
| 6 | + function redirect() { |
| 7 | + const queryString = window.location.search; |
| 8 | + const urlParams = new URLSearchParams(queryString); |
| 9 | + const code = urlParams.get("code"); |
| 10 | + const state = urlParams.get("state"); |
| 11 | + if (code != null) { |
| 12 | + fetch("https://gitee.com/oauth/token?grant_type=authorization_code&code=" + code + "&client_id=" + client_id + "&redirect_uri=" + gitee_redirect_uri + "&client_secret=" + client_secret, { |
| 13 | + method: "POST" |
| 14 | + }) |
| 15 | + .then(response => response.text()) |
| 16 | + .then(result => { |
| 17 | + resp = JSON.parse(result); |
| 18 | + console.log("result", resp); |
| 19 | + if (resp.access_token != null) { |
| 20 | + //获取用户信息 |
| 21 | + localStorage.removeItem("gitee_user"); |
| 22 | + const token = resp.access_token; |
| 23 | + fetch("https://gitee.com/api/v5/user?access_token=" + token, { |
| 24 | + method: "GET" |
| 25 | + }).then(response => response.text()) |
| 26 | + .then(result => { |
| 27 | + resp = JSON.parse(result); |
| 28 | + console.log("result", resp); |
| 29 | + localStorage.setItem("access_token", token); |
| 30 | + localStorage.setItem("gitee_user", result); |
| 31 | + window.location.href = state + "?timestamp=" + Date.now(); |
| 32 | + }); |
| 33 | + |
| 34 | + } else if ("invalid_grant_accessibility" == resp.error) { |
| 35 | + window.location.href = "https://gitee.com/oauth/authorize?client_id=" + client_id + "&redirect_uri=" + gitee_redirect_uri + "&response_type=code"; |
| 36 | + } |
| 37 | + }) |
| 38 | + .catch(error => console.error(error)); |
40 | 39 | } |
| 40 | + } |
41 | 41 |
|
42 | | - function getUserInfo() { |
43 | | - let access_token = checkAccessToken() |
44 | | - if (access_token == null) { |
45 | | - return |
46 | | - } |
47 | | - let user = localStorage.getItem('gitee_user') |
48 | | - if (user != null) { |
49 | | - return JSON.parse(user) |
50 | | - } |
| 42 | + function getUserInfo() { |
| 43 | + let access_token = checkAccessToken(); |
| 44 | + if (access_token == null) { |
| 45 | + return; |
51 | 46 | } |
| 47 | + let user = localStorage.getItem("gitee_user"); |
| 48 | + if (user != null) { |
| 49 | + return JSON.parse(user); |
| 50 | + } |
| 51 | + } |
52 | 52 |
|
53 | | - function checkAccessToken() { |
54 | | - let access_token = localStorage.getItem('access_token') |
55 | | - console.log("access_token", access_token); |
56 | | - if (access_token == null) { |
57 | | - window.location.href = 'https://gitee.com/oauth/authorize?client_id=' + client_id + '&redirect_uri=' + gitee_redirect_uri + '&response_type=code&state=' + location.href; |
58 | | - return null; |
59 | | - } else { |
60 | | - return access_token; |
61 | | - } |
| 53 | + function checkAccessToken() { |
| 54 | + let access_token = localStorage.getItem("access_token"); |
| 55 | + console.log("access_token", access_token); |
| 56 | + if (access_token == null) { |
| 57 | + window.location.href = "https://gitee.com/oauth/authorize?client_id=" + client_id + "&redirect_uri=" + gitee_redirect_uri + "&response_type=code&state=" + location.href; |
| 58 | + return null; |
| 59 | + } else { |
| 60 | + return access_token; |
62 | 61 | } |
| 62 | + } |
63 | 63 |
|
64 | | - function checkStar(owner, repo,failCallback) { |
65 | | - //文档首页不校验 |
66 | | - if (location.pathname === repo || location.pathname === repo + '/') { |
67 | | - return; |
68 | | - } |
69 | | - let access_token = checkAccessToken() |
70 | | - if (access_token == null) { |
71 | | - return |
72 | | - } |
73 | | - fetch("https://gitee.com/api/v5/user/starred/" + owner + "/" + repo + "?access_token=" + access_token, { |
74 | | - method: "GET" |
75 | | - }) |
76 | | - .then(response => { |
77 | | - console.log("check result", response); |
78 | | - if (response.status == 204) { |
79 | | - } else if (response.status == 401) { |
80 | | - localStorage.removeItem("access_token") |
81 | | - checkStar(owner, repo) |
82 | | - } else if (response.status == 404) { |
83 | | - if(failCallback!= null){ |
84 | | - failCallback() |
85 | | - }else { |
86 | | - //替换 html body中 class为 content-wrapper 中的内容,适配vuepress |
87 | | - replaceHtml(` |
| 64 | + function checkStar(owner, repo, failCallback) { |
| 65 | + //文档首页不校验 |
| 66 | + if (location.pathname === repo || location.pathname === repo + "/") { |
| 67 | + return; |
| 68 | + } |
| 69 | + let access_token = checkAccessToken(); |
| 70 | + if (access_token == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + fetch("https://gitee.com/api/v5/user/starred/" + owner + "/" + repo + "?access_token=" + access_token, { |
| 74 | + method: "GET" |
| 75 | + }) |
| 76 | + .then(response => { |
| 77 | + console.log("check result", response); |
| 78 | + if (response.status == 204) { |
| 79 | + } else if (response.status == 401) { |
| 80 | + localStorage.removeItem("access_token"); |
| 81 | + checkStar(owner, repo); |
| 82 | + } else if (response.status == 404) { |
| 83 | + if (failCallback != null) { |
| 84 | + failCallback(); |
| 85 | + } else { |
| 86 | + //替换 html body中 class为 content-wrapper 中的内容,适配vuepress |
| 87 | + replaceHtml(` |
88 | 88 | <div> |
89 | 89 | 检测到您还未 Star 本项目,暂时无法访问本页面。<br/> |
90 | 90 | 请先前往:<a target="_blank" href="https://gitee.com/${owner}/${repo}">https://gitee.com/${owner}/${repo}</a> 完成操作,再尝试刷新当前页面。 |
91 | 91 | </div> |
92 | | - `) |
93 | | - } |
94 | | - } |
95 | | - }) |
96 | | - .catch(error => console.error(error)); |
| 92 | + `); |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | + .catch(error => console.error(error)); |
| 97 | + } |
| 98 | + |
| 99 | + function checkAuthorize(owner, repo, file, failCallback) { |
| 100 | + let access_token = checkAccessToken(); |
| 101 | + if (access_token == null) { |
| 102 | + return; |
97 | 103 | } |
| 104 | + let user = getUserInfo(); |
98 | 105 |
|
99 | | - function checkAuthorize(owner, repo, file,failCallback) { |
100 | | - let access_token = checkAccessToken() |
101 | | - if (access_token == null) { |
102 | | - return |
103 | | - } |
104 | | - let user = getUserInfo() |
105 | | - |
106 | | - //浮层遮挡,提示授权检测中 |
107 | | - // 创建一个遮罩层元素 |
108 | | - const mask = document.createElement('div'); |
109 | | - mask.style.position = 'fixed'; |
110 | | - mask.style.top = '0'; |
111 | | - mask.style.left = '0'; |
112 | | - mask.style.width = '100%'; |
113 | | - mask.style.height = '100%'; |
114 | | - mask.style.backgroundColor = 'rgba(255, 255, 255, 0.8)'; // 不透明白色背景 |
115 | | - mask.style.backdropFilter = 'blur(10px)'; // 添加玻璃效果 |
116 | | - mask.style.zIndex = '9999'; // 确保遮罩层在最上层 |
| 106 | + //浮层遮挡,提示授权检测中 |
| 107 | + // 创建一个遮罩层元素 |
| 108 | + const mask = document.createElement("div"); |
| 109 | + mask.style.position = "fixed"; |
| 110 | + mask.style.top = "0"; |
| 111 | + mask.style.left = "0"; |
| 112 | + mask.style.width = "100%"; |
| 113 | + mask.style.height = "100%"; |
| 114 | + mask.style.backgroundColor = "rgba(255, 255, 255, 0.8)"; // 不透明白色背景 |
| 115 | + mask.style.backdropFilter = "blur(10px)"; // 添加玻璃效果 |
| 116 | + mask.style.zIndex = "9999"; // 确保遮罩层在最上层 |
117 | 117 |
|
118 | 118 | // 创建一个提示框元素 |
119 | | - const promptBox = document.createElement('div'); |
120 | | - promptBox.style.position = 'fixed'; |
121 | | - promptBox.style.top = '50%'; |
122 | | - promptBox.style.left = '50%'; |
123 | | - promptBox.style.transform = 'translate(-50%, -50%)'; |
124 | | - promptBox.style.padding = '20px'; |
125 | | - promptBox.style.backgroundColor = '#fff'; |
126 | | - promptBox.style.borderRadius = '5px'; |
127 | | - promptBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.3)'; |
128 | | - promptBox.style.zIndex = '10000'; // 确保提示框在遮罩层之上 |
129 | | - |
130 | | - // 设置提示框内容 |
131 | | - promptBox.innerHTML = '<p>授权检测中,请稍候...</p>'; |
132 | | - // 将遮罩层和提示框添加到页面中 |
133 | | - document.body.appendChild(mask); |
134 | | - document.body.appendChild(promptBox); |
135 | | - |
136 | | - fetch("https://gitee.com/api/v5/repos/" + owner + "/" + repo + "/contents/" + file + "?access_token=" + access_token, { |
137 | | - method: "GET" |
138 | | - }) |
139 | | - .then(response => { |
140 | | - console.log("check result", response); |
141 | | - if (response.status == 200) { |
142 | | - //解析body |
143 | | - response.json().then(function (data) { |
144 | | - console.log("check result", data); |
145 | | - //解析base64, |
146 | | - let content = atob(data.content) |
147 | | - //转成utf8 |
148 | | - content = decodeURIComponent(escape(content)) |
149 | | - |
150 | | - console.log("check result", content); |
151 | | - //解析json |
152 | | - let json = JSON.parse(content) |
153 | | - localStorage.setItem('userCount', Object.keys(json).length); |
154 | | - if (json[user?.login] != null) { |
155 | | - console.log("check result", "ok"); |
156 | | - // 授权检测完成后,移除遮罩层和提示框 |
157 | | - document.body.removeChild(mask); |
158 | | - document.body.removeChild(promptBox); |
159 | | - } else { |
160 | | - if(failCallback!= null){ |
161 | | - failCallback() |
162 | | - }else{ |
163 | | - replaceHtml(`<div> |
| 119 | + const promptBox = document.createElement("div"); |
| 120 | + promptBox.style.position = "fixed"; |
| 121 | + promptBox.style.top = "50%"; |
| 122 | + promptBox.style.left = "50%"; |
| 123 | + promptBox.style.transform = "translate(-50%, -50%)"; |
| 124 | + promptBox.style.padding = "20px"; |
| 125 | + promptBox.style.backgroundColor = "#fff"; |
| 126 | + promptBox.style.borderRadius = "5px"; |
| 127 | + promptBox.style.boxShadow = "0 0 10px rgba(0, 0, 0, 0.3)"; |
| 128 | + promptBox.style.zIndex = "10000"; // 确保提示框在遮罩层之上 |
| 129 | + |
| 130 | + // 设置提示框内容 |
| 131 | + promptBox.innerHTML = "<p>授权检测中,请稍候...</p>"; |
| 132 | + // 将遮罩层和提示框添加到页面中 |
| 133 | + document.body.appendChild(mask); |
| 134 | + document.body.appendChild(promptBox); |
| 135 | + |
| 136 | + fetch("https://gitee.com/api/v5/repos/" + owner + "/" + repo + "/contents/" + file + "?access_token=" + access_token, { |
| 137 | + method: "GET" |
| 138 | + }) |
| 139 | + .then(response => { |
| 140 | + console.log("check result", response); |
| 141 | + if (response.status == 200) { |
| 142 | + //解析body |
| 143 | + response.json().then(function(data) { |
| 144 | + console.log("check result", data); |
| 145 | + //解析base64, |
| 146 | + let content = atob(data.content); |
| 147 | + //转成utf8 |
| 148 | + content = decodeURIComponent(escape(content)); |
| 149 | + |
| 150 | + console.log("check result", content); |
| 151 | + //解析json |
| 152 | + let json = JSON.parse(content); |
| 153 | + localStorage.setItem("userCount", Object.keys(json).length); |
| 154 | + let user = json[user?.login]; |
| 155 | + //用户未授权 |
| 156 | + if (!user) { |
| 157 | + if (failCallback != null) { |
| 158 | + failCallback("unauthorized"); |
| 159 | + } else { |
| 160 | + replaceHtml(`<div> |
164 | 161 | 未经授权暂时无法访问该页面。<br/> |
165 | 162 | </div> |
166 | | - `) |
167 | | - } |
168 | | - } |
169 | | - }); |
170 | | - } else { |
171 | | - console.log("check fail result", response) |
172 | | - localStorage.removeItem("access_token") |
173 | | - checkAuthorize(owner, repo, file) |
174 | | - } |
175 | | - }) |
176 | | - .catch(error => console.error(error)); |
177 | | - } |
| 163 | + `); |
| 164 | + } |
| 165 | + return; |
| 166 | + } |
178 | 167 |
|
179 | | - function replaceHtml(html) { |
180 | | - //替换 html body中 class为 content-wrapper 中的内容,适配vuepress |
181 | | - d = document.querySelector(".content-wrapper") |
182 | | - if (d == null) { |
183 | | - //适配 astro |
184 | | - d = document.querySelector(".main-pane") |
185 | | - } |
186 | | - if (d == null) { |
187 | | - d = document.getElementsByTagName("body")[0] |
| 168 | + //判断是否过期,expireTime为字符串,例如"2025-03-04" |
| 169 | + if (user?.expireTime && new Date(user.expireTime).getTime() < Date.now()) { |
| 170 | + if (failCallback != null) { |
| 171 | + failCallback("expired"); |
| 172 | + } else { |
| 173 | + replaceHtml(`<div> |
| 174 | + 授权已过期。<br/> |
| 175 | + </div> |
| 176 | + `); |
| 177 | + } |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + console.log("check result", "ok"); |
| 182 | + // 授权检测完成后,移除遮罩层和提示框 |
| 183 | + document.body.removeChild(mask); |
| 184 | + document.body.removeChild(promptBox); |
| 185 | + |
| 186 | + }); |
| 187 | + } else { |
| 188 | + console.log("check fail result", response); |
| 189 | + localStorage.removeItem("access_token"); |
| 190 | + checkAuthorize(owner, repo, file); |
188 | 191 | } |
189 | | - d.innerHTML = html |
| 192 | + }) |
| 193 | + .catch(error => console.error(error)); |
| 194 | + } |
| 195 | + |
| 196 | + function replaceHtml(html) { |
| 197 | + //替换 html body中 class为 content-wrapper 中的内容,适配vuepress |
| 198 | + d = document.querySelector(".content-wrapper"); |
| 199 | + if (d == null) { |
| 200 | + //适配 astro |
| 201 | + d = document.querySelector(".main-pane"); |
| 202 | + } |
| 203 | + if (d == null) { |
| 204 | + d = document.getElementsByTagName("body")[0]; |
190 | 205 | } |
| 206 | + d.innerHTML = html; |
| 207 | + } |
191 | 208 |
|
192 | | - window.checkStar = checkStar; |
193 | | - window.redirect = redirect; |
194 | | - window.checkAuthorize = checkAuthorize; |
| 209 | + window.checkStar = checkStar; |
| 210 | + window.redirect = redirect; |
| 211 | + window.checkAuthorize = checkAuthorize; |
195 | 212 | } |
0 commit comments