-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
152 lines (125 loc) · 4.91 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
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
document.addEventListener('DOMContentLoaded', function() {
var urlForm = document.getElementById('urlForm');
var urlInput = document.getElementById('urlInput');
var statsContainer = document.getElementById('statsContainer');
var captchaFrame = document.getElementById('captchaFrame');
const iframe = document.getElementById('captchaFrame');
iframe.sandbox = 'allow-scripts';
// Get the current active tab URL
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var currentTab = tabs[0];
var url = currentTab.url;
urlInput.value = url;
getWebsiteStats(url);
});
urlForm.addEventListener('submit', function(event) {
event.preventDefault();
var url = urlInput.value.trim();
if (url === '') {
return;
}
getWebsiteStats(url);
});
function getWebsiteStats(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.similarweb.com/website/' + url.replace('https://www.', '') + '/#overview', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var parser = new DOMParser();
var responseDoc = parser.parseFromString(xhr.responseText, 'text/html');
var statsElement = responseDoc.querySelector('.wa-overview__row');
var captchaElement = responseDoc.querySelector('.sec-container'); // Replace with the actual class of the CAPTCHA element
if (statsElement) {
statsContainer.innerHTML = '';
statsContainer.appendChild(statsElement);
captchaFrame.style.display = 'none';
} else if (captchaElement) {
statsContainer.innerHTML = 'CAPTCHA verification required.';
captchaFrame.src = 'https://www.similarweb.com/website/' + url.replace('https://', '') + '/#overview';
captchaFrame.style.display = 'block';
} else {
statsContainer.innerHTML = 'Stats not found.';
captchaFrame.style.display = 'none';
}
}
};
xhr.send();
}
var notLoadingLink = document.getElementById('notLoadingLink');
// Get the current tab's URL
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var currentTab = tabs[0];
var url = currentTab.url;
var modifiedUrl = getModifiedUrl(url);
notLoadingLink.href = modifiedUrl;
});
function getModifiedUrl(url) {
// Remove 'https://' and everything after the first slash
var modifiedUrl = url.replace('https://', '');
modifiedUrl = modifiedUrl.split('/')[0];
// Construct the final URL
return 'https://www.similarweb.com/website/' + modifiedUrl + '/#overview';
}
// Open new tab when the link is clicked
notLoadingLink.addEventListener('click', function(event) {
event.preventDefault();
window.open(notLoadingLink.href, '_blank');
});
// Handle premium subscription
var checkoutButton = document.getElementById('checkoutButton');
checkoutButton.addEventListener('click', function(event) {
event.preventDefault();
var email = prompt('Please enter your email:');
if (email) {
createSubscription(email);
}
});
function createSubscription(email) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://dashboard.stripe.com/create-subscription', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
alert('Subscription created successfully!');
// Perform any necessary UI updates or actions for premium users
} else {
alert('Subscription creation failed. Please try again later.');
}
}
};
xhr.send(JSON.stringify({ email: email }));
}
var customerId = null;
// Create a customer on the server
fetch('https://YOUR_GLITCH_PROJECT_URL/create-customer', {
method: 'POST',
})
.then((response) => response.json())
.then((data) => {
customerId = data.customer.id;
})
.catch((error) => console.error('Error:', error));
// Create a premium plan session on the server
function createPremiumSession() {
fetch('https://YOUR_GLITCH_PROJECT_URL/create-session', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ customerId: customerId }),
})
.then((response) => response.json())
.then((data) => {
stripe.redirectToCheckout({
sessionId: data.sessionId,
});
})
.catch((error) => console.error('Error:', error));
}
// Handle the "Buy Premium" button click event
document.getElementById('buyPremiumBtn').addEventListener('click', function() {
createPremiumSession();
});
});