forked from arnoudkooi/ServiceNow-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewdata.js
213 lines (173 loc) · 6.66 KB
/
viewdata.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let url = getUrlVars('url');
let g_ck = getUrlVars('g_ck');
let tableName = getUrlVars('tablename');
let sysId = getUrlVars('sysid');
let dtViewData;
getViewData();
function getViewData() {
if (!tableName) { //try to find table and sys_id in workspace
let myurl = new URL(response.frameHref)
let parts = myurl.pathname.split("/");
let idx = parts.indexOf("sub") // show subrecord if available
if (idx != -1) parts = parts.slice(idx);
idx = parts.indexOf("record")
if (idx > -1 && parts.length >= idx + 2) {
tableName = parts[idx + 1];
sysId = parts[idx + 2];
}
}
if (!(tableName && sysId)) {
setViewData([]);
return true;
}
let myurl = url + '/api/now/ui/meta/' + tableName;
snuFetch(g_ck, myurl, null, function (metaData) {
let query = '';
if (sysId)
query = '&sysparm_query=sys_id%3D' + sysId;
let myurl = url + '/api/now/table/' + tableName + '?sysparm_display_value=all&sysparm_limit=1' + query;
snuFetch(g_ck, myurl, null, function (jsn) {
let dataView = [];
let propObj = {};
propObj.name = "#TABLE / SYS_ID";
propObj.meta = {
"label": "#TABLE / SYS_ID",
"type": "TABLE"
};
propObj.display_value = "<a class='referencelink' href='" + url + "/" + tableName + ".do?sys_id=" + sysId + "' target='_blank'>" + tableName + " / " + sysId + "</a>";
propObj.link = url + "/" + tableName + ".do?sys_id=" + sysId;
propObj.value = tableName + " / " + sysId;
dataView.push(propObj);
let rows = {}
try {
rows = jsn.result[0];
} catch (e) {
rows = { "Error": { "display_value": e.message, "value": "Record data not retrieved." } };
}
for (let key in rows) {
let propObj = {};
if (!rows.hasOwnProperty(key)) continue;
let display_value = rows[key].display_value;
let link = propObj.link = rows[key].link;
if (link) {
let linksplit = link.split('/');
let href = url + '/' + linksplit[6] + '.do?sys_id=' + linksplit[7];
if (!display_value) display_value = '[Deleted reference or empty display value]';
display_value = "<a href='" + href + "' target='_blank'>" + display_value + "</a>";
}
propObj.name = key;
propObj.meta = (metaData && metaData != "error") ? metaData.result.columns[key] : { "label": "Error" };
propObj.display_value = display_value;
propObj.value = (display_value != rows[key].value) ? rows[key].value : '';
dataView.push(propObj);
}
setViewData(dataView);
});
});
}
//set or refresh datatable with ServiceNow tables
function setViewData(nme) {
Object.entries(nme).forEach( // add check of empty fields to be able to filter out
([key, obj]) => {
nme[key].hasdata = (obj.value || obj.display_value) ? "hasdata" : "";
if (!nme[key].link ) {
let escpd = escape(nme[key].display_value);
if (nme[key].display_value != escpd)
nme[key].display_value = '<pre style="white-space: pre-wrap; max-width:600px;"><code>' + escpd + '</code></pre>'
}
}
);
if (dtViewData) dtTables.destroy();
//$('#dataexplore').html(DOMPurify.sanitize(nme));
dtViewData = $('#dataexplore').DataTable({
"aaData": nme,
"createdRow": function (row, data, index) {
console.log(row, data, index);
},
"aoColumns": [
{ "mDataProp": "meta.label" },
{ "mDataProp": "name" },
{
mRender: function (data, type, row) {
let reference = "<div class='refname'>" + row?.meta?.reference + "</div>";
if (reference.includes('undefined')) reference = '';
return row?.meta?.type + reference;
},
"bSearchable": true,
"mDataProp": "meta.type"
},
{ "mDataProp": "value" },
{ "mDataProp": "display_value" },
{ "mDataProp": "hasdata" }
],
"language": {
"info": "Matched: _TOTAL_ of _MAX_ fields | Hold down CMD or CTRL to keep window open after clicking a link ",
"infoFiltered": "",
"infoEmpty": "No matches found"
},
"bLengthChange": false,
"bSortClasses": false,
"scrollY": "75vh",
"scrollCollapse": true,
"paging": false,
"dom": 'rti<"btns"B>',
"buttons": [
"copyHtml5"
]
});
dtViewData.column(5).visible(false);
$('#tbxdataexplore').keyup(function () {
let srch = ($('#cbxhideempty').prop('checked') ? "hasdata " : "") + $('#tbxdataexplore').val();
dtViewData.search(srch, true).draw();
}).focus().trigger('keyup');
$('#cbxhideempty').change(function (e) {
let srch = ($('#cbxhideempty').prop('checked') ? "hasdata " : "") + $('#tbxdataexplore').val();
dtViewData.search(srch, true).draw();
});
$('a.referencelink').click(function () {
event.preventDefault();
chrome.tabs.create({ "url": $(this).attr('href'), "active": !(event.ctrlKey || event.metaKey) });
});
$('#waitingdataexplore').hide();
}
//Function to query Servicenow API
function snuFetch(token, url, post, callback) {
let hdrs = {
'Cache-Control': 'no-cache',
'Accept': 'application/json',
'Content-Type': 'application/json'
};
if (token) //only for instances with high security plugin enabled
hdrs['X-UserToken'] = token;
let requestInfo = {
method: 'get',
headers: hdrs
}
if (post) {
requestInfo.method = 'PUT';
requestInfo.body = post;
}
fetch(url, requestInfo)
.then(response => response.json())
.then(data => {
callback(data);
});
}
function getUrlVars(key) {
let vars = {};
let parts = window.location.href.replace(
/[?&]+([^=&]+)=([^&]*)/gi,
function (m, key, value) {
vars[key] = decodeURIComponent(value);
}
);
return vars[key];
}
function escape(htmlStr) {
if (!htmlStr) return '';
return htmlStr.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}