-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
001-patch-to-apply.js
71 lines (63 loc) · 2.13 KB
/
001-patch-to-apply.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
// Helper function to check if a URL contains a certain keyword
function checkUrl(uri, keyword) {
return uri?.includes?.(keyword);
}
// Handlers for different types of URLs
const urlHandlers = {
'soundcloud.com': uri => {
const MySoundCloudClass = class extends SoundCloudWidget {
url = uri
}
return system.registerWidget(new MySoundCloudClass())
},
'youtube.com/': uri => handleYoutube(uri),
'youtu.be': uri => handleYoutube(uri),
// '://': uri => system.registerWidget(new iFrameWidget(uri))
};
// Add image handlers in a loop to reduce redundancy
['.png', '.jpg', '.gif', '.jpeg', '.webp'].forEach(ext => {
urlHandlers[ext] = uri => system.registerWidget(new ImageViewerWidget(uri));
});
// Handler for YouTube URLs
function handleYoutube(uri) {
let updatedUrl = uri;
try {
let url = new URL(updatedUrl);
updatedUrl = url.toString();
} catch (error) {
console.error('Invalid URL:', updatedUrl);
}
return system.registerWidget(new YoutubePlayerWidget("", {tracks: [updatedUrl]}));
}
// Refactored tryInvokeHandlerForUri function
function tryInvokeHandlerForUri(uri) {
console.warn('tryInvokeHandlerForUri', {uri});
// Check if the URL matches any of the handlers
for (let keyword in urlHandlers) {
if (checkUrl(uri, keyword)) {
return urlHandlers[keyword](uri);
}
}
// If none of the above, check if it's a generic URL and handle it as an iframe
if (uri?.includes?.("://")) {
return system.registerWidget(new iFrameWidget(uri));
}
// If no handler matched, try to invoke a command
try {
let result = InvokableCommands[uri];
if (typeof result === 'string') {
return this.tryInvokeHandlerForUri(result);
} else if (typeof result === 'function') {
return result.call(result);
} else {
console.warn(`tryInvoke ${uri}`, typeof result, result);
}
if (result) {
return result;
}
} catch(e) {
console.error("error trying string as command ", e);
throw e;
}
return -1000;
}