-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
198 lines (179 loc) · 4.54 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!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.kontent.ai/js-api/custom-element/v1/custom-element.min.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;
}
#linklist {
width:100%;
}
td {
overflow:hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 3px 5px;
}
tr:hover:not(:first-child) {
background-color: #EEE;
}
.status, .richtext {
text-align:center;
}
.title, .link {
max-width:250px;
}
.richtext {
max-width:100px;
}
#check_links {
margin: 5px;
}
.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>
</div>
<script>
var item_id = '';
var requestRepeater = '';
var numberOfLinks = 0;
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(data);
console.log(JSON.parse(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;
$('#linklist').remove();
$('#element').append('<table id="linklist"><tr><th>Link title</th><th>Link url</th><th>Rich text</th><th>Working link</th></tr></table>');
while((result = reg.exec(value)) !== null) {
checkLink(result[1].split('"')[1],result[2],name);
}
}
function checkLink(url,title,name) {
numberOfLinks++;
var data = {
"url": url
}
$.ajax({
url: "https://amend.cz/checker.php", // add a link to your own request service
type: "POST",
data: data,
dataType: 'text',
success: function (data) {
showLinkResult(url,title,name,data);
numberOfLinks--;
}
});
}
function showLinkResult(url,title,name,code) {
var ok = (code<400);
var row = '<tr class="row"><td class="title" title="'+title+'"><strong>'+title+'</strong></td><td class="link" title="'+url+'"><a target="_blank" href="'+url+'">'+url+'</a></td><td class="richtext" title="'+name+'">'+name+'</td><td class="status" title="'+code+'">'+(ok?"✔️":"❌")+'</td></tr>';
$('#linklist').append(row);
updateSize();
}
function showError(message) {
$('#check_links').hide();
$('#element').text(message);
}
</script>
</body>
</html>