-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_checker.html
169 lines (152 loc) · 4 KB
/
link_checker.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Link checker</title>
<!-- Include jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<!-- Include the Custom Elements API-->
<script src="https://app.kenticocloud.com/js-api/custom-element.js"></script>
<link rel="stylesheet" href="./custom-element.css" />
<!-- Custom element CSS styles -->
<style>
/* We recommended you always set margin to zero to avoid problems when displaying the element in UI */
@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic);
html{
font-family:sans-serif;
-ms-text-size-adjust:100%;
-webkit-text-size-adjust:100%;
}
body {
margin: 0;
overflow-y: hidden;
overflow-x: hidden;
}
.disabled_overlay {
position: fixed;
background-color: #777;
z-index: 10;
cursor: not-allowed;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0.1;
}
</style>
</head>
<body>
<div class="disabled_overlay"></div>
<div id="element">
<button id="check_links">Check links</button>
<span id="test"></span>
</div>
<script>
var item_id = '';
var requestRepeater = '';
function updateDisabled(disabled) {
if (disabled) {
$('.disabled_overlay').show();
}
else {
$('.disabled_overlay').hide();
}
}
function setup(value) {
$("#check_links").click(function(e) {
checkLinks();
});
}
function updateSize() {
var height = 100;
try {
height = parseInt($("html").height());
} catch (err) {
console.error(err);
}
CustomElement.setHeight(height);
}
function initCustomElement() {
try {
CustomElement.init((element, _context) => {
item_id = _context.item.id;
var configError = true;
if (element.config) {
if (element.config.request_repeater) {
requestRepeater = element.config.request_repeater;
configError = false;
}
else {
showError("Your configuration doesn't contain the url of a request repeater. Please check the documentation of this element.");
}
}
if (!configError) {
setup(element.value);
}
updateDisabled(element.disabled);
updateSize();
});
CustomElement.onDisabledChanged(updateDisabled);
} catch (err) {
console.error(err);
updateDisabled(true);
}
}
initCustomElement();
function checkLinks() {
numberOfLinks = 0;
var urlItem = requestRepeater+"?system.id="+item_id;
$.ajax({
url: urlItem,
type: "GET",
dataType: 'text',
success: function (data) {
//console.log(JSON.parse(String(data)));
iterateRichTexts(JSON.parse(String(data)));
}
});
}
function iterateRichTexts(data) {
var elements = data.items[0].elements;
for (var element in elements) {
if (elements[element].type == "rich_text") {
findLinksIn(elements[element].name, elements[element].value);
}
}
}
function findLinksIn(name,value) {
var reg = /<a([^>]+)>(.+?)<\/a>/g;
var result;
while((result = reg.exec(value)) !== null) {
checkLink(result[1].split('"')[1],result[2],name);
}
}
function checkLink(url,title,name) {
console.log(url);
var data = {
"url": url
};//'{"url":"' + url + '"}';
console.log(data);
$.ajax({
url: "https://djp6a10pzi.execute-api.eu-central-1.amazonaws.com/default/urlChecker", // develop this service
type: "POST",
data: JSON.stringify(data),
dataType: "json",
success: function (data) {
showLinkResult(url,title,name,data);
console.log(data);
}
});
}
function showLinkResult(url,title,name,code) {
//document.write("<span>" + url + " " + code + "</span>");
$('#test').text(code);
console.log(code);
}
function showError(message) {
$('#check_links').hide();
$('#element').text(message);
}
</script>
</body>
</html>