browser-extension-settings 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/common.ts CHANGED
@@ -1,3 +1,3 @@
1
- export const version = 11
1
+ export const besVersion = 13
2
2
  export const openButton = `<svg viewBox="0 0 60.2601318359375 84.8134765625" version="1.1" xmlns="http://www.w3.org/2000/svg" class=" glyph-box" style="height: 9.62969px; width: 6.84191px;"><g transform="matrix(1 0 0 1 -6.194965820312518 77.63671875)"><path d="M66.4551-35.2539C66.4551-36.4746 65.9668-37.5977 65.0391-38.4766L26.3672-76.3672C25.4883-77.1973 24.4141-77.6367 23.1445-77.6367C20.6543-77.6367 18.7012-75.7324 18.7012-73.1934C18.7012-71.9727 19.1895-70.8496 19.9707-70.0195L55.5176-35.2539L19.9707-0.488281C19.1895 0.341797 18.7012 1.41602 18.7012 2.68555C18.7012 5.22461 20.6543 7.12891 23.1445 7.12891C24.4141 7.12891 25.4883 6.68945 26.3672 5.81055L65.0391-32.0312C65.9668-32.959 66.4551-34.0332 66.4551-35.2539Z"></path></g></svg>`
3
3
  export const openInNewTabButton = `<svg viewBox="0 0 72.127685546875 72.2177734375" version="1.1" xmlns="http://www.w3.org/2000/svg" class=" glyph-box" style="height: 8.19958px; width: 8.18935px;"><g transform="matrix(1 0 0 1 -12.451127929687573 71.3388671875)"><path d="M84.5703-17.334L84.5215-66.4551C84.5215-69.2383 82.7148-71.1914 79.7852-71.1914L30.6641-71.1914C27.9297-71.1914 26.0742-69.0918 26.0742-66.748C26.0742-64.4043 28.1738-62.4023 30.4688-62.4023L47.4609-62.4023L71.2891-63.1836L62.207-55.2246L13.8184-6.73828C12.9395-5.85938 12.4512-4.73633 12.4512-3.66211C12.4512-1.31836 14.5508 0.878906 16.9922 0.878906C18.1152 0.878906 19.1895 0.488281 20.0684-0.439453L68.5547-48.877L76.6113-58.0078L75.7324-35.2051L75.7324-17.1387C75.7324-14.8438 77.7344-12.6953 80.127-12.6953C82.4707-12.6953 84.5703-14.6973 84.5703-17.334Z"></path></g></svg>`
@@ -99,6 +99,20 @@ export const activeExtension = (id: string) => {
99
99
  }
100
100
  }
101
101
 
102
+ export const activeExtensionList = () => {
103
+ const extensionListContainer = $(".extension_list_container")
104
+ if (extensionListContainer) {
105
+ addClass(extensionListContainer, "bes_active")
106
+ }
107
+ }
108
+
109
+ export const deactiveExtensionList = () => {
110
+ const extensionListContainer = $(".extension_list_container")
111
+ if (extensionListContainer) {
112
+ removeClass(extensionListContainer, "bes_active")
113
+ }
114
+ }
115
+
102
116
  const createInstalledExtension = (installedExtension: InstalledExtension) => {
103
117
  const div = createElement("div", {
104
118
  class: "installed_extension",
@@ -120,7 +134,7 @@ const updateRelatedExtensions = (container: HTMLElement) => {
120
134
  container.innerHTML = ""
121
135
 
122
136
  for (const relatedExtension of relatedExtensions) {
123
- console.log(relatedExtension)
137
+ // console.log(relatedExtension)
124
138
 
125
139
  if (isInstalledExtension(relatedExtension.id)) {
126
140
  continue
@@ -154,7 +168,7 @@ export function createExtensionList(installedExtensions: InstalledExtension[]) {
154
168
  class: "installed_extension_list",
155
169
  })
156
170
  for (const installedExtension of installedExtensions) {
157
- console.log(installedExtension)
171
+ // console.log(installedExtension)
158
172
 
159
173
  if (isInstalledExtension(installedExtension.id)) {
160
174
  continue
package/lib/settings.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  import {
7
7
  $,
8
8
  $$,
9
+ addClass,
9
10
  addElement,
10
11
  addEventListener,
11
12
  addStyle,
@@ -19,8 +20,10 @@ import {
19
20
  createExtensionList,
20
21
  addCurrentExtension,
21
22
  activeExtension,
23
+ activeExtensionList,
24
+ deactiveExtensionList,
22
25
  } from "./extension-list"
23
- import { version } from "./common"
26
+ import { besVersion } from "./common"
24
27
 
25
28
  const prefix = "browser_extension_settings_"
26
29
 
@@ -64,7 +67,7 @@ const settingsContainerId = prefix + "container_" + randomId
64
67
  const settingsElementId = prefix + "main_" + randomId
65
68
  const getSettingsElement = () => $("#" + settingsElementId)
66
69
  const getSettingsStyle: () => string = () =>
67
- styleText
70
+ (styleText as string)
68
71
  .replace(/browser_extension_settings_container/gm, settingsContainerId)
69
72
  .replace(/browser_extension_settings_main/gm, settingsElementId)
70
73
  const storageKey = "settings"
@@ -94,22 +97,44 @@ export function getSettingsValue(key: string): boolean | string | undefined {
94
97
  : settingsTable[key]?.defaultValue
95
98
  }
96
99
 
97
- const modalHandler = (event) => {
98
- let target = event.target as HTMLElement
100
+ const closeModal = () => {
99
101
  const settingsContainer = getSettingsContainer()
100
102
  if (settingsContainer) {
101
- while (target !== settingsContainer && target) {
102
- target = target.parentNode as HTMLElement
103
- }
103
+ settingsContainer.style.display = "none"
104
+ }
104
105
 
105
- if (target === settingsContainer) {
106
- return
106
+ removeEventListener(document, "click", onDocumentClick)
107
+ removeEventListener(document, "keydown", onDocumentKeyDown)
108
+ }
109
+
110
+ const onDocumentClick = (event: Event) => {
111
+ const target = event.target as HTMLElement
112
+ if (target?.closest(`.${prefix}container`)) {
113
+ return
114
+ }
115
+
116
+ closeModal()
117
+ }
118
+
119
+ const onDocumentKeyDown = (event: KeyboardEvent) => {
120
+ if (event.defaultPrevented) {
121
+ return // 如果事件已经在进行中,则不做任何事。
122
+ }
123
+
124
+ switch (event.key) {
125
+ case "Escape": {
126
+ // 按“ESC”键时要做的事。
127
+ closeModal()
128
+ break
107
129
  }
108
130
 
109
- settingsContainer.style.display = "none"
131
+ default: {
132
+ return
133
+ } // 什么都没按就退出吧。
110
134
  }
111
135
 
112
- removeEventListener(document, "click", modalHandler)
136
+ // 取消默认动作,从而避免处理两次。
137
+ event.preventDefault()
113
138
  }
114
139
 
115
140
  async function updateOptions() {
@@ -148,9 +173,9 @@ function getSettingsContainer() {
148
173
  const container = $(`.${prefix}container`)
149
174
  if (container) {
150
175
  const theVersion = Number.parseInt(container.dataset.besVersion || "0", 10)
151
- if (theVersion < version) {
176
+ if (theVersion < besVersion) {
152
177
  container.id = settingsContainerId
153
- container.dataset.besVersion = String(version)
178
+ container.dataset.besVersion = String(besVersion)
154
179
  }
155
180
 
156
181
  return container
@@ -159,14 +184,16 @@ function getSettingsContainer() {
159
184
  return addElement(doc.body, "div", {
160
185
  id: settingsContainerId,
161
186
  class: `${prefix}container`,
162
- "data-bes-version": version,
187
+ "data-bes-version": besVersion,
188
+ style: "display: none;",
163
189
  })
164
190
  }
165
191
 
166
192
  function getSettingsWrapper() {
193
+ const container = getSettingsContainer()
167
194
  return (
168
- $(`.${prefix}container .${prefix}wrapper`) ||
169
- addElement(getSettingsContainer(), "div", {
195
+ $(`.${prefix}wrapper`, container) ||
196
+ addElement(container, "div", {
170
197
  class: `${prefix}wrapper`,
171
198
  })
172
199
  )
@@ -209,7 +236,7 @@ function createSettingsElement() {
209
236
  textContent: "Settings",
210
237
  class: "navigation_go_previous",
211
238
  onclick() {
212
- console.log(1)
239
+ activeExtensionList()
213
240
  },
214
241
  })
215
242
 
@@ -223,8 +250,10 @@ function createSettingsElement() {
223
250
  const item = settingsTable[key]
224
251
  if (!item.type || item.type === "switch") {
225
252
  const switchOption = createSwitchOption(item.title, {
226
- async onchange(event) {
227
- await saveSattingsValue(key, event.target.checked)
253
+ async onchange(event: Event) {
254
+ if (event.target) {
255
+ await saveSattingsValue(key, event.target.checked)
256
+ }
228
257
  },
229
258
  })
230
259
 
@@ -238,21 +267,24 @@ function createSettingsElement() {
238
267
  const options2 = addElement(settingsMain, "div", {
239
268
  class: "option_groups",
240
269
  })
241
- let timeoutId
270
+ let timeoutId: number | undefined
242
271
  addElement(options2, "textarea", {
243
272
  placeholder: `/* Custom rules for internal URLs, matching URLs will be opened in new tabs */`,
244
- onkeyup(event) {
273
+ onkeyup(event: Event) {
274
+ const textArea = event.target as HTMLTextAreaElement
245
275
  if (timeoutId) {
246
276
  clearTimeout(timeoutId)
247
- timeoutId = null
277
+ timeoutId = undefined
248
278
  }
249
279
 
250
280
  timeoutId = setTimeout(async () => {
251
281
  const host = location.host
252
- await saveSattingsValue(
253
- `customRulesForCurrentSite_${host}`,
254
- event.target.value.trim()
255
- )
282
+ if (textArea) {
283
+ await saveSattingsValue(
284
+ `customRulesForCurrentSite_${host}`,
285
+ textArea.value.trim()
286
+ )
287
+ }
256
288
  }, 100)
257
289
  },
258
290
  })
@@ -306,14 +338,14 @@ function addSideMenu() {
306
338
  $("#browser_extension_side_menu") ||
307
339
  addElement(doc.body, "div", {
308
340
  id: "browser_extension_side_menu",
309
- "data-bes-version": version,
341
+ "data-bes-version": besVersion,
310
342
  })
311
343
 
312
344
  const button = $("button[data-bes-version]", menu)
313
345
 
314
346
  if (button) {
315
347
  const theVersion = Number.parseInt(button.dataset.besVersion || "0", 10)
316
- if (theVersion >= version) {
348
+ if (theVersion >= besVersion) {
317
349
  return
318
350
  }
319
351
 
@@ -322,7 +354,7 @@ function addSideMenu() {
322
354
 
323
355
  addElement(menu, "button", {
324
356
  type: "button",
325
- "data-bes-version": version,
357
+ "data-bes-version": besVersion,
326
358
  title: "设置",
327
359
  onclick() {
328
360
  setTimeout(showSettings, 1)
@@ -337,8 +369,10 @@ export async function showSettings() {
337
369
  await updateOptions()
338
370
  settingsContainer.style.display = "block"
339
371
 
340
- addEventListener(document, "click", modalHandler)
372
+ addEventListener(document, "click", onDocumentClick)
373
+ addEventListener(document, "keydown", onDocumentKeyDown)
341
374
  activeExtension(settingsOptions.id)
375
+ deactiveExtensionList()
342
376
  }
343
377
 
344
378
  export const initSettings = async (options: SettingsOptions) => {
package/lib/style.scss CHANGED
@@ -187,6 +187,7 @@
187
187
  }
188
188
 
189
189
  .option_groups textarea {
190
+ font-size: 12px;
190
191
  margin: 10px 0 10px 0;
191
192
  height: 100px;
192
193
  width: 100%;
@@ -359,3 +360,25 @@
359
360
  #browser_extension_side_menu button:active {
360
361
  opacity: 100%;
361
362
  }
363
+
364
+ @media (max-width: 500px) {
365
+ #browser_extension_settings_container {
366
+ right: 10px;
367
+
368
+ .extension_list_container {
369
+ display: none;
370
+ }
371
+ .extension_list_container.bes_active {
372
+ display: block;
373
+ }
374
+ .extension_list_container.bes_active + div {
375
+ display: none;
376
+ }
377
+ }
378
+
379
+ #browser_extension_settings_main {
380
+ a.navigation_go_previous {
381
+ display: block;
382
+ }
383
+ }
384
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-settings",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Settings module for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.ts",