-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpluginScript.js
More file actions
110 lines (92 loc) · 3.46 KB
/
pluginScript.js
File metadata and controls
110 lines (92 loc) · 3.46 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
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
const sessionKey = 'pixivSession';
const siteUrl = '{siteUrl}';
const saveSession = (session) => {
window.localStorage.setItem(sessionKey, session);
}
const loadSession = () => {
window.localStorage.getItem(sessionKey);
}
const getInitConfig = () => JSON.parse(document.getElementById('init-config').value)
const getToken = (callback) => {
return grecaptcha.enterprise.ready(() => {
const pixivInitConfig = getInitConfig()
grecaptcha.enterprise.execute(pixivInitConfig['pixivAccount.recaptchaEnterpriseScoreSiteKey'], { action: 'accounts/login' }).then(callback)
})
}
const getFormData = () => {
const form = new FormData(formBlock);
const data = Object.fromEntries(form);
const inputs = formBlock.getElementsByTagName('input');
return Object.assign(data, {
login_id: inputs[1]?.value,
password: inputs[2]?.value
})
}
const buttonEvent = (e) => getToken(recaptcha_enterprise_score_token => {
const data = Object.assign(getFormData(), { recaptcha_enterprise_score_token });
console.log(data)
showErrors(['Loading...'])
fetch(siteUrl + "/login", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(res => res.json())
.then(res => {
if (res.ok) {
newButton.style.background = '#68bdb4';
console.log('pixivSession:', res.session);
saveSession(res.session);
window.location.replace(siteUrl + `/session/${res.session}`);
}
else {
const prevColor = newButton.style.background
newButton.style.background = '#ff2563';
setTimeout(() => {
newButton.style.background = prevColor;
}, 5000)
if (res.errors?.length) {
showErrors(res.errors)
}
}
})
})
const createNewButton = () => {
const dpixivButton = document.createElement('button');
dpixivButton.className = loginButton.className;
dpixivButton.innerHTML = 'Login to dpixiv';
dpixivButton.type = 'button';
dpixivButton.style.cssText = 'margin-bottom: -10px; margin-top: 8px;'
dpixivButton.addEventListener('click', buttonEvent)
return dpixivButton
}
const createErrorBlock = () => {
const block = document.createElement('ul');
block.style.cssText = "margin: 8px auto; padding-left: 0px; list-style-type: none;"
return block
}
const createErrorItem = (text) => {
const err = document.createElement('li');
err.style.cssText = "text-align: left; color: rgb(255, 255, 255); line-height: 1.5; font-size: 14px; padding: 13px 10px; margin: 0px auto 2px; width: 280px; background-color: rgba(187, 22, 22, 0.3); border-radius: 4px; border: medium none;"
err.innerHTML = text;
return err
}
const showErrors = (errors) => {
errorBlock.innerHTML = "";
for (const error of errors) {
errorBlock.appendChild(createErrorItem(error))
}
}
const getLoginButton = () => formBlock.getElementsByTagName('button')[0];
const putNewBlock = (loginButton, NewButton) => {
loginButton.parentNode.insertBefore(NewButton, loginButton);
}
const formBlock = document.forms[0]
const errorBlock = createErrorBlock()
const loginButton = getLoginButton()
const newButton = createNewButton()
if (loginButton) {
putNewBlock(loginButton, errorBlock)
putNewBlock(loginButton, newButton)
}