-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_injection_funcs.js
103 lines (89 loc) · 3.85 KB
/
js_injection_funcs.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
function getApiUrl(url) {
const resourceTypes = ['facult', 'profession', 'track', 'course', 'topic', 'lesson', 'task'];
let apiUrl = '';
let matchedResource = '';
for (let i = 0; i < resourceTypes.length; i++) {
const resourceRegex = new RegExp(`/${resourceTypes[i]}s?/([\\w\\-]+)(/$|$)`, 'i');
if (resourceRegex.test(url)) {
const resourceId = url.match(resourceRegex)[1];
matchedResource = resourceTypes[i];
apiUrl = `https://prestable.pierce-admin.praktikum.yandex-team.ru/content/${matchedResource}s/${resourceId}/breadcrumbs/`;
break;
}
}
if (!matchedResource) {
throw new Error('Invalid URL or unsupported resource type');
}
return apiUrl;
}
// For debugging requests in a browser JS-bookmarklet
function fetchBreadcrumbsBookmarklet(url, download_to_filename) {
try {
const apiUrl = getApiUrl(url);
const response = fetch(apiUrl);
const data = response.json();
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = download_to_filename || 'download.json';
downloadLink.click();
const jsonDisplay = document.createElement('pre');
jsonDisplay.textContent = JSON.stringify(data, null, 2);
document.body.appendChild(jsonDisplay);
return {data};
} catch (error) {
return {error: error.message};
}
}
function fetchBreadcrumbs(url, download_to_filename, callback) {
try {
const apiUrl = getApiUrl(url);
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = download_to_filename || 'download.json';
downloadLink.click();
const jsonDisplay = document.createElement('pre');
jsonDisplay.textContent = JSON.stringify(data, null, 2);
document.body.appendChild(jsonDisplay);
callback({ data }); // Success callback
})
.catch(error => {
callback({ error: error.message }); // Error in fetch/JSON processing
});
} catch (error) {
callback({ error: error.message }); // Error in getApiUrl or other
}
}
// Substitute callback for this function when debugging in JS console:
// function myCallback(result) {
// console.log('Callback result:', result);
// }
function fetchApiUrl(args) {
const { apiUrl, download_to_filename, modifyDocument = false, callback } = args;
try {
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = download_to_filename || 'download.json';
downloadLink.click();
if (modifyDocument) {
const jsonDisplay = document.createElement('pre');
jsonDisplay.textContent = JSON.stringify(data, null, 2);
document.body.appendChild(jsonDisplay);
}
callback({data}); // Success callback
})
.catch(error => {
callback({error: error.message}); // Error in fetch/JSON processing
});
} catch (error) {
callback({error: error.message}); // Error in fetchApiUrl
}
}