forked from hansvangent/Bookmarklets-for-SEO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robots.js
67 lines (63 loc) · 2.05 KB
/
robots.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
javascript:(function() {
var body = document.getElementsByTagName('body')[0];
var jq = document.createElement('script');
jq.src = 'https://code.jquery.com/jquery-1.7.min.js';
body.appendChild(jq);
function init() {
if (typeof jQuery !== 'undefined') {
var robots = document.createElement('div');
robots.id = '_robots_txt';
robots.style.display = 'none';
body.appendChild(robots);
$('#_robots_txt').load('https://' + window.location.hostname + '/robots.txt', function (response, status, xhr) {
var msg = '';
if (status === 'error') {
msg = 'Sorry, there was an error retrieving the robots.txt file: ' + xhr.status + ' (' + xhr.statusText + ')';
alert(msg);
}
else {
var robotsTxt = $('#_robots_txt').html(),
msg = '',
blockedTxt = 'This page is blocked for at least one robot.',
allBlocked = /^User-agent: (\*|[a-zA-Z0-9]*)$\n^Disallow: \/$/gim,
pathMatch = window.location.pathname.match(/^(.+[\/]?)[^\/]+$/),
pageBlocked = new RegExp('^Disallow: ' + window.location.pathname + '$', 'gim'),
match = false;
if (robotsTxt.match(allBlocked) || robotsTxt.match(pageBlocked)) {
msg = blockedTxt;
}
else if (pathMatch !== null && robotsTxt.match(new RegExp('Disallow: ' + pathMatch[1] + '$', 'gim'))){
msg = blockedTxt;
}
else {
if (pathMatch) {
var matches = robotsTxt.match(/^Disallow: (.*)$/gim),
matchStr = '';
if (matches) {
for (var i=0; i<matches.length; i++) {
matchStr = matches[i].substr(10);
var matchRegExp = new RegExp(matchStr.replace(/\*/gim, '.*'));
if (pathMatch[1].indexOf(matchStr) !== -1 || pathMatch[1].match(matchRegExp)) {
match = true;
break;
}
}
}
}
if (match) {
msg = blockedTxt;
}
else {
msg = 'This page is not blocked.';
}
}
confirm(msg + '\r\n\r\nWould you like to see the robots.txt file?') ? alert(robotsTxt) : '';
}
});
}
else {
setTimeout(init, 50);
}
};
init();
})();