-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.js
More file actions
185 lines (152 loc) · 7.22 KB
/
checker.js
File metadata and controls
185 lines (152 loc) · 7.22 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
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
let proxyServer = "https://api.allorigins.win/get?url=";
const tagListEmpty = `
<div class="card">
<div class="card-body">
<p class="text-center mt-3">No tags found. Please try entering a URL and search. </p>
</div>
</div>`
function formatResponseTime(milliseconds) {
if (milliseconds < 1000) {
return `${milliseconds.toFixed(2)} ms`;
} else if (milliseconds < 60000) {
const seconds = milliseconds / 1000;
return `${seconds.toFixed(2)} sec`;
} else {
const minutes = milliseconds / 60000;
return `${minutes.toFixed(2)} min`;
}
}
async function checkSEO() {
const url = document.getElementById('url-input').value;
// Clear previous results
document.getElementById('tag-list').innerHTML = '';
document.getElementById('status').innerHTML = '';
document.getElementById("loadingStatus").innerHTML = "Approaching website ..."
if (!url || !checkUrl(url)) {
document.getElementById('status').textContent = ` Please enter a valid URL, Eg : (https://www.example.com)`;
document.getElementById('tag-list').innerHTML = tagListEmpty;
return;
}
try {
// Use a proxy server to bypass CORS restrictions (AllOrigins or your custom proxy)
document.getElementById("loadingStatus").innerHTML = `Setting proxy server (${proxyServer}) ...`;
const proxyUrl = proxyServer;
const targetUrl = encodeURIComponent(url);
document.getElementById("loadingStatus").innerHTML = "Bypassing CORS restrictions (AllOrigins or your custom proxy) ...";
const startTime = performance.now();
// Fetch the HTML content of the given URL through the proxy
const response = await fetch(proxyUrl + targetUrl);
if (!response.ok) {
throw new Error('Failed to fetch the URL');
};
document.getElementById("loadingStatus").innerHTML = "Getting response from the website ...";
const data = await response.json();
const endTime = performance.now();
const responseTime = endTime - startTime;
document.getElementById("loadingStatus").innerHTML = "Got response 200 and start analyzing html...";
const html = data.contents;
const parser = new DOMParser();
const doc = await parser.parseFromString(html, 'text/html');
document.getElementById("loadingStatus").innerHTML = "Processing result for display...";
// Get all <meta> tags from the document
const metaTags = doc.getElementsByTagName('meta');
const tagList = document.getElementById('tag-list');
let jsonResults = [];
// Loop through the meta tags and create JSON objects
Array.from(metaTags).forEach(tag => {
const name = tag.getAttribute('name');
const property = tag.getAttribute('property');
const content = tag.getAttribute('content');
const httpEquiv = tag.getAttribute('http-equiv');
// Create the base object for the meta tag
let tagObject = {
"Tag name": name || property || httpEquiv || "Unknown name",
"Content": content || "No Content",
"isMatch": content ? true : false,
"IsFacebook": (property && property.includes('og:')),
"IsTwitter": (name && name.includes('twitter')),
"isGoogle": (name && name.includes('google')),
"fullTag": tag.outerHTML
};
// Push the tag object to JSON results
jsonResults.push(tagObject);
// Create a tag item to display on the page
const tagItem = document.createElement('div');
tagItem.classList.add('card', 'mb-3');
// Create tag name heading with icons
const tagName = document.createElement('h5');
tagName.classList.add('card-header', 'd-flex', 'justify-content-between');
// Create a div for icons to be aligned on the right
const iconDiv = document.createElement('div');
iconDiv.classList.add('d-flex', 'align-items-center'); // Ensuring icons are aligned properly
// Add icon if isMatch is Yes
let iconHTML = "";
if (tagObject["isMatch"]) {
iconHTML = `<i class="bi bi-check-circle text-success ms-auto"></i> `;
} else {
iconHTML = `<i class="bi bi-dash-circle ms-auto"></i> `;
}
// Add Facebook icon if it's a Facebook tag
if (tagObject["IsFacebook"]) {
iconHTML += `<i class="bi bi-facebook text-primary ms-2"></i> `;
}
// Add Google icon if it's a Google tag
if (tagObject["isGoogle"]) {
iconHTML += `<i class="bi bi-google text-danger ms-2"></i> `;
}
// Add Twitter icon if it's a Twitter tag
if (tagObject["IsTwitter"]) {
iconHTML += `<i class="bi bi-twitter text-info ms-2"></i> `;
}
// Set the tag name with icons
tagName.innerHTML = `${tagObject["Tag name"]} ${iconHTML}`;
tagItem.appendChild(tagName);
// Create description for other details
const tagDescription = document.createElement('div');
tagDescription.classList.add('card-body');
tagDescription.innerHTML = `
<pre>${tagObject["fullTag"].replace(/</g, "<").replace(/>/g, ">")}</pre> <!-- Display full tag using <pre> -->
`;
tagItem.appendChild(tagDescription);
// Append the tag item to the list
tagList.appendChild(tagItem);
});
// Display the overall status
if (jsonResults.length > 0) {
document.getElementById('result').innerHTML = `<span class="badge bg-success rounded-pill">${jsonResults.length}</span> meta tags found in ${formatResponseTime(responseTime)}`;
} else {
document.getElementById('status').textContent = 'No meta tags found.';
}
} catch (error) {
console.error('Error:', error);
document.getElementById('status').textContent = 'An error occurred while fetching the URL.';
}
}
document.getElementById('tag-list').innerHTML = tagListEmpty;
document.getElementById('checkBtn').addEventListener('click', async () => {
document.getElementById('loading').innerHTML = ` <div class="spinner-border mt-5" role="status"><span class="visually-hidden">Loading...</span></div> </br> Searching meta tags , it may take while, we have to wait for response </br>`;
await checkSEO();
document.getElementById('loading').innerHTML = '';
document.getElementById("loadingStatus").innerHTML = "";
});
function checkUrl(input) {
try {
return new URL(input);
} catch (e) {
return false;
}
}
$(document).ready(function () {
let options = {
html: true,
content: $('[data-name="popover-content"]'),
}
let exampleEl = document.getElementById('setProxy');
let popover = new bootstrap.Popover(exampleEl, options);
$('#saveProxyServer').click(function (event) {
event.preventDefault();
proxyServer = checkUrl($('#proxyServerInput').val());
if (!proxyServer) return;
popover.hide();
});
})