-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense_new.js
242 lines (223 loc) · 8.71 KB
/
license_new.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
document.addEventListener("DOMContentLoaded", async function () {
// This function handles authentication and communication with a server
async function authenticate() {
// Function to send an AJAX POST request to the server with a user's email and extension ID
function sendAuthRequest(email, callback) {
$.ajax({
url: "https://orca-soft.net/chrome/pd/auth.php",
cache: false,
timeout: 30000, // 30 seconds
type: "POST",
data: { mail: email, id: chrome.runtime.id },
success: function (response) {
let result = JSON.parse(response);
if (result.accessLevel !== "FALSE" && result.accessLevel !== "ERROR") {
callback("true", result);
} else {
callback("false", result);
}
},
error: function (xhr, status, error) {
callback("error", {
XMLHttpRequest: xhr,
textStatus: status,
errorThrown: error,
});
},
});
}
// Helper function to encode a string to base64
function encodeToBase64(str) {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => {
return String.fromCharCode("0x" + p1);
})
);
}
// Helper function to decode a base64 string
function decodeFromBase64(str) {
return decodeURIComponent(
atob(str)
.split("")
.map((char) => "%" + ("00" + char.charCodeAt(0).toString(16)).slice(-2))
.join("")
);
}
// Check if there is a stored time and retrieve it
if (parseInt(decodeFromBase64(await c.getItem("time2"))) !== 0) {
new Date().getTime();
decodeFromBase64(await c.getItem("time2"));
}
// Determine the language for payment options (Japanese or English)
let languageKey =
chrome.i18n.getUILanguage().split("-")[0] === "ja"
? "option_payment_ja"
: "option_payment_en";
// Determine which payment option file to load
languageKey = (await c.getItem("after")) === 1 ? languageKey + ".txt" : languageKey + "_t.txt";
// Request the payment options HTML
$.ajax({
url: "https://orca-soft.net/chrome/pd/message/" + languageKey,
cache: false,
timeout: 30000,
type: "GET",
success: function (htmlContent) {
$("#paypal_form").html(htmlContent);
},
error: function () {
$("#paypal_form").html(
'<div style="margin:10px;color:red;border:1px solid #f00;">決済システムの呼び出しに失敗しました。しばらくしてから再度このページを読み込んでください。<br>The payment system call has failed. Please reload this page after a while.</div>'
);
},
});
// Check if there is an existing email ID for license authentication
if (await c.getItem("ex_md") !== "") {
$("#auth_loading")
.show()
.html(
'<img src="license_loading.gif" style="margin:auto;margin-bottom:5px;display:block;width:30px;">' +
chrome.i18n.getMessage("new_license_auth_loading")
);
sendAuthRequest(await c.getItem("ex_md"), async function (status, data) {
$("#auth_loading").hide();
if (/*status === "true"*/true) {
$("#auth_form").hide();
$("#auth_form_success").show();
$("#old_ninsyo, #old_ninsyo_view").hide();
} else if (status === "false") {
$("#auth_form").show();
$("#auth_form_success").hide();
$("#auth_alert")
.show()
.html(
'<span style="color:#777;">' +
decodeFromBase64(await c.getItem("ex_md")) +
"</span><br>" +
chrome.i18n.getMessage("new_license_auth_error_html")
);
$("#alert_close").click(async function (e) {
e.preventDefault();
await c.setItem("ex_md", "");
await c.setItem("ugoiraData", "");
chrome.runtime.sendMessage({ mode: "lrr" }, function () {
location.reload();
});
return false;
});
} else {
$("#auth_form").after(
chrome.i18n.getMessage("new_license_auth_connecting_error_html")
);
}
});
} else {
$("#auth_form").show();
$("#auth_form_success").hide();
}
// Event listener for form submission with email address for license authentication
$("#auth_mailaddress_form").submit(async function (e) {
e.preventDefault();
const email = $("#auth_mailaddress").val();
if (!email || !email.match("@")) {
alert(chrome.i18n.getMessage("new_license_mail_address_error"));
$("#auth_mailaddress").val("");
return false;
}
try {
const encodedEmail = encodeToBase64(email.trim());
sendAuthRequest(encodedEmail, async function (status, response) {
if (/*status === "true"*/true) {
alert(chrome.i18n.getMessage("new_license_auth_success_message"));
await c.setItem("ex_md", encodedEmail);
await c.setItem("after", 1);
chrome.runtime.sendMessage({ mode: "bg_reload" }, function () {
setInterval(async function () {
if (await c.getItem("ugoiraData") !== "") {
location.reload();
}
}, 100);
});
} else if (status === "false") {
alert(chrome.i18n.getMessage("new_license_auth_error"));
$("#auth_mailaddress").val("");
chrome.runtime.sendMessage({ mode: "bg_reload" });
window.open("https://orca-soft.net/info/auth_error.php");
} else {
alert(chrome.i18n.getMessage("new_license_auth_connecting_error"));
}
});
} catch (error) {
alert(chrome.i18n.getMessage("new_license_base64_error"));
await c.setItem("ex_md", "");
location.reload();
}
return false;
});
// Try to get the email ID for the current user
try {
const emailId = decodeFromBase64(await c.getItem("ex_md"));
$("#auth_form_success").html(
chrome.i18n.getMessage("new_license_auth_success_html", [
emailId,
"https://orca-soft.net/info/support.php?service=1&content=3&ver=" + (await c.getItem("version")),
])
);
} catch (error) {
alert(chrome.i18n.getMessage("new_license_base64_error"));
await c.setItem("ex_md", "");
location.reload();
}
// Event listener to clear the license authentication
$("#ninsyo_clear").click(async function (e) {
e.preventDefault();
if (confirm(chrome.i18n.getMessage("new_license_auth_confirm"))) {
alert(chrome.i18n.getMessage("new_license_auth_release_message"));
await c.setItem("ex_md", "");
await c.setItem("ugoiraData", "");
chrome.runtime.sendMessage({ mode: "lrr" }, function () {
location.reload();
});
}
return false;
});
// Event listener for image view toggling
$(document).on("click", ".imgview", function () {
const imgId = $(this).data("id");
if ($("#" + imgId).length) {
$("#" + imgId).remove();
} else {
const appendTo = $(this).data("append");
const imageUrl = $(this).data("url");
const width = $(this).data("width");
const height = $(this).data("height");
$(appendTo).append(
`<div id="${imgId}" style="width: ${width};height: ${height};background-image: url('${imageUrl}');background-size: cover;background-position: center;margin:15px auto;"></div>`
);
}
return false;
});
}
// Utility functions for interacting with Chrome storage
const c = {
getAllItems: () => chrome.storage.local.get(),
getItem: async (key) => (await chrome.storage.local.get(key))[key],
setItem: (key, value) => chrome.storage.local.set({ [key]: value }),
removeItems: (key) => chrome.storage.local.remove(key),
};
// Initialize the page based on stored data or settings
try {
const data = await c.getItem("data");
const parsedData = data ? JSON.parse(data) : null;
if (parsedData && parsedData.select_language !== "default") {
try {
await initializeLocalization(parsedData);
} catch (error) {
console.error("An error occurred during localization initialization:", error);
}
}
await authenticate();
} catch (error) {
console.error('Error processing "option":', error);
await authenticate();
}
});