MCPcopy Create free account
hub / github.com/ClydeTime/Surge / getItem

Method getItem

Script/Env.js:97–137  ·  view source on GitHub ↗
(keyName = new String, defaultValue = null)

Source from the content-addressed store, hash-verified

95 }
96
97 getItem(keyName = new String, defaultValue = null) {
98 let keyValue = defaultValue
99 // 如果以 @
100 switch (keyName.startsWith('@')) {
101 case true:
102 const { key, path } = keyName.match(/^@(?<key>[^.]+)(?:\.(?<path>.*))?$/)?.groups
103 keyName = key
104 let value = this.getItem(keyName, {})
105 if (typeof value !== "object") value = {}
106 keyValue = this.lodash_get(value, path)
107 try {
108 keyValue = JSON.parse(keyValue)
109 } catch (e) {
110 // do nothing
111 }
112 break
113 default:
114 switch (this.platform()) {
115 case 'Surge':
116 case 'Loon':
117 case 'Stash':
118 case 'Egern':
119 case 'Shadowrocket':
120 keyValue = $persistentStore.read(keyName)
121 break
122 case 'Quantumult X':
123 keyValue = $prefs.valueForKey(keyName)
124 break
125 default:
126 keyValue = this.data?.[keyName] || null
127 break
128 }
129 try {
130 keyValue = JSON.parse(keyValue)
131 } catch (e) {
132 // do nothing
133 }
134 break
135 }
136 return keyValue ?? defaultValue
137 }
138
139 setItem(keyName = new String, keyValue = new String) {
140 let result = false

Callers 1

setItemMethod · 0.45

Calls 4

lodash_getMethod · 0.45
parseMethod · 0.45
platformMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected