-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (103 loc) · 4.15 KB
/
script.js
File metadata and controls
112 lines (103 loc) · 4.15 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
111
112
jQuery(document).ready(function($) {
// Check URL status using HEAD request
async function checkUrl(url) {
try {
const response = await fetch(url, {
method: 'HEAD',
mode: 'no-cors',
cache: 'no-cache',
redirect: 'follow',
referrerPolicy: 'no-referrer'
});
return true;
} catch (error) {
return false;
}
}
// Process URLs in batches
async function processBatch(urls, batchSize = 5) {
const results = [];
for (let i = 0; i < urls.length; i += batchSize) {
const batch = urls.slice(i, i + batchSize);
const promises = batch.map(async (url, index) => {
const position = i + index;
const row = `<tr>
<td>${position + 1}</td>
<td><a href="${url}" target="_blank">${url}</a></td>
<td><span class="checking">Checking...</span></td>
</tr>`;
$('#fbg-results-table tbody').append(row);
const isWorking = await checkUrl(url);
const statusCell = $(`#fbg-results-table tr:eq(${position + 1}) td:last`);
if (isWorking) {
statusCell.html('<span class="success">✓</span>');
} else {
statusCell.html('<span class="error">✗</span>');
}
return { url, isWorking };
});
const batchResults = await Promise.all(promises);
results.push(...batchResults);
if (i + batchSize < urls.length) {
await new Promise(resolve => setTimeout(resolve, 200));
}
}
return results;
}
$('#fbg-form').on('submit', function(e) {
e.preventDefault();
var website_url = $('#fbg-url').val().replace(/\/+$/, '');
$.ajax({
url: fbg_ajax.ajax_url,
type: 'POST',
data: {
action: 'fbg_generate_urls',
website_url: website_url,
nonce: fbg_ajax.nonce
},
beforeSend: function() {
$('#fbg-results').html('<div class="fbg-loading">Generating URLs...</div>');
},
success: async function(response) {
if (response.success) {
var html = `
<table id="fbg-results-table">
<thead>
<tr>
<th>#</th>
<th>Generated URL</th>
<th>Status</th>
</tr>
</thead>
<tbody></tbody>
</table>`;
$('#fbg-results').html(html);
var validUrls = response.data.filter(url => /^https?:\/\/[^\s]+$/.test(url));
await processBatch(validUrls);
var linksText = validUrls.join('\n');
$('#fbg-results').append(`
<div class="fbg-links-section">
<textarea id="fbg-links-textarea" readonly>${linksText}</textarea>
<button id="fbg-copy-button">Copy All Links</button>
</div>`
);
} else {
$('#fbg-results').html('<p class="fbg-error">' + response.data + '</p>');
}
},
error: function() {
$('#fbg-results').html('<p class="fbg-error">An error occurred. Please try again.</p>');
}
});
});
// Copy functionality
$(document).on('click', '#fbg-copy-button', function() {
var textarea = document.getElementById('fbg-links-textarea');
textarea.select();
document.execCommand('copy');
$(this).text('Copied!').prop('disabled', true);
setTimeout(() => {
$('#fbg-copy-button').text('Copy All Links').prop('disabled', false);
}, 2000);
});
});