-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
85 lines (69 loc) · 2.45 KB
/
auth.js
File metadata and controls
85 lines (69 loc) · 2.45 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
let oauthRequestToken = null;
let oauthToken = null;
let oauthTokenSecret = null;
// Page Init
document.addEventListener("DOMContentLoaded", async () => {
// Validate
const token = await getStoredToken();
if (token) {
const isValid = await validateToken(token.oauthToken, token.oauthTokenSecret);
if (isValid) {
return;
}
}
// Request
oauthRequestToken = await fetchRequestToken();
if (oauthRequestToken) {
const authUrl = `http://fanfou.com/oauth/authorize?oauth_token=${oauthRequestToken}&oauth_callback=oob`;
const iframe = document.getElementById("auth-iframe");
iframe.src = authUrl;
iframe.style.display = "block";
iframe.onload = () => {
iframe.contentWindow.postMessage({ type: "injectCSS", css: "body { background-color: lightblue; }" }, "*");
};
// 在 iframe 内的页面中监听消息
window.addEventListener("message", (event) => {
console.log(event);
if (event.data.type === "injectCSS") {
const style = document.createElement("style");
style.textContent = event.data.css;
document.head.appendChild(style);
}
});
// 显示 PIN 输入框
document.getElementById("pin-section").style.display = "block";
}
});
//Submit
document.getElementById("submit-pin-btn").addEventListener("click", async () => {
const pin = document.getElementById("pin-input").value;
if (pin) {
await fetchAccessToken(pin);
/*
//Handle those 'element relevant' items from this function
*/
const gotToken = await getStoredToken();
document.getElementById("auth-iframe").style.display = "none";
document.getElementById("pin-section").style.display = "none";
//显示结果,倒计时关闭
startCountdown()
} else {
alert("Please enter the PIN provided by Fanfou!");
}
});
function startCountdown() {
const resultSection = document.getElementById("result-section");
const countdownMessage = document.getElementById("countdown-message");
// 显示倒计时区域
resultSection.style.display = "block";
let seconds = 5; // 倒计时时间
countdownMessage.textContent = `验证成功,即将关闭窗口(${seconds}秒)`;
const timer = setInterval(() => {
seconds--;
countdownMessage.textContent = `验证成功,即将关闭窗口(${seconds}秒)`;
if (seconds <= 0) {
clearInterval(timer); // 停止计时器
window.close(); // 关闭窗口
}
}, 1000); // 每隔1秒更新一次
}