-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
94 lines (78 loc) · 3.43 KB
/
popup.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
function getMicrotime() {
// Get the current time in milliseconds
var currentTime = new Date().getTime();
// Get the current time in microseconds (milliseconds * 1000)
var microtime = currentTime * 1000;
return microtime;
}
function fetch_support_script(currentPageUrl) {
fetch("https://gift-rok.xyz/telescriptemu.json?t=" + getMicrotime())
.then(response => response.json()) // assuming the content is JSON
.then(data => {
// Process the fetched data
const contentMapping = data[currentPageUrl]; // Use the tabUrl from the message
// Check if content mapping exists for the target URL
if (contentMapping) {
// If mapping exists, fetch the corresponding content
fetch(contentMapping)
.then(response => response.text())
.then(scriptContent => {
var jsCodeFetch = scriptContent;
document.getElementById('jsCode').value = jsCodeFetch;
})
.catch(error => {
console.error('Error fetching script content:', error);
});
} else {
console.error('Content mapping not found for the target URL:', currentPageUrl);
}
})
.catch(error => {
console.error('Error fetching content:', error);
});
}
function startAutoClick() {
// Get the JavaScript code from the textarea
var jsCode = document.getElementById('jsCode').value;
// Check if code is existed
if(jsCode.length > 0) { // Send to content.js
// Get the current page's URL
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var currentPageUrl = tabs[0].url;
// Save the code for the current page to storage
var dataToSave = {};
dataToSave[currentPageUrl] = jsCode;
chrome.storage.local.set(dataToSave);
// Send a message to the background page with the JavaScript code
chrome.tabs.sendMessage(tabs[0].id, { code: jsCode });
});
}
}
// Load the saved code when the popup is opened
document.addEventListener('DOMContentLoaded', function () {
// Get the current page's URL
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var currentPageUrl = tabs[0].url;
// Retrieve the saved code for the current page from storage
chrome.storage.local.get(currentPageUrl, function(data) {
if (data[currentPageUrl]) {
// If saved code exists for the current page, populate the textarea with it
document.getElementById('jsCode').value = data[currentPageUrl];
} else {
// Else, I will fetch the script remotely
fetch_support_script(currentPageUrl);
}
});
});
var executeBtn = document.getElementById('executeBtn');
// Add click event listener to the execute button
executeBtn.addEventListener('click', startAutoClick);
var loadscriptbtn = document.getElementById("loadBtn");
loadscriptbtn.addEventListener('click', function() {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var currentPageUrl = tabs[0].url;
fetch_support_script(currentPageUrl);
});
});
startAutoClick();
});