-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.js
More file actions
88 lines (86 loc) · 3.13 KB
/
addon.js
File metadata and controls
88 lines (86 loc) · 3.13 KB
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
const getRuntimeAddon = () => self
var RuntimeAddon = JSB.defineClass("RuntimeAddon : JSExtension", {
sceneWillConnect: async function() {
MNUtil.addObserverForAddonBroadcast(self, 'onAddonBroadcast:')
Runtime.emitLifecycle("sceneWillConnect", getRuntimeAddon())
},
sceneDidDisconnect: function() {
Runtime.emitLifecycle("sceneDidDisconnect", getRuntimeAddon())
},
notebookWillOpen: function(notebookId) {
self.notebookid = notebookId
Runtime.emitLifecycle("notebookWillOpen", notebookId, getRuntimeAddon())
},
notebookWillClose: function(notebookId) {
Runtime.emitLifecycle("notebookWillClose", notebookId, getRuntimeAddon())
},
documentDidOpen: function(documentController) {
Runtime.emitLifecycle("documentDidOpen", documentController, getRuntimeAddon())
},
documentWillClose: function(documentController) {
Runtime.emitLifecycle("documentWillClose", documentController, getRuntimeAddon())
},
controllerWillLayoutSubviews: function(controller) {
Runtime.emitLifecycle("controllerWillLayoutSubviews", controller, getRuntimeAddon())
},
/**
*
* @param {{userInfo:{message:string},window:UIWindow,name:string}} notification
* @returns
*/
onAddonBroadcast: async function(notification) {
let url = `marginnote4app://addon/${notification.userInfo.message}`
let parsed = MNUtil.parseURL(url)
// console.log(parsed)
let routeName = parsed.pathComponents[0]
if (!routeName) {
return
}
if (routeName === "installAddon") {
let addURL = parsed.params.url
let addonId = parsed.params.id
let addonName = parsed.params.name??addonId
let addonNameString = addonName ? ("\n\n"+addonName) : ""
if (addURL && addURL.endsWith(".mnaddon")) {
let confirmed = await MNUtil.confirm("MN Utils",Locale.at("confirmInstallAddon")+addonNameString)
if (!confirmed) {
return
}
let res = await MNUtil.installAddonFromURL(addURL, addonId)
if (res.success) {
MNUtil.alert(Locale.at("installSuccess"),Locale.at("pleaseRestartMNManually"))
}else{
MNUtil.alert(Locale.at("installFailed"),res.error)
}
}else{
MNUtil.alert(Locale.at("installFailed"),Locale.at("invalidURL"))
}
return
}
if (routeName in Runtime.routeHandlers) {
return Runtime.handleRoute(routeName, parsed, notification, getRuntimeAddon())
}
},
queryAddonCommandStatus: function() {
return Runtime.emitLifecycle("queryAddonCommandStatus", getRuntimeAddon()).find(Boolean)
}
}, {
addonDidConnect: function() {
Runtime.emitLifecycle("addonDidConnect", getRuntimeAddon())
},
addonWillDisconnect: function() {
Runtime.emitLifecycle("addonWillDisconnect", getRuntimeAddon())
},
applicationWillEnterForeground: function() {
Runtime.emitLifecycle("applicationWillEnterForeground", getRuntimeAddon())
},
applicationDidEnterBackground: function() {
Runtime.emitLifecycle("applicationDidEnterBackground", getRuntimeAddon())
}
})
JSB.newAddon = function(mainPath) {
Runtime.init(mainPath)
Locale.init(mainPath)
MNUtil.init(mainPath)
return RuntimeAddon
}