chore: convert extension apis to TypeScript (#18688)

Converts extensions-related files to TS
This commit is contained in:
Shelley Vohr 2019-06-14 07:52:24 -07:00 committed by GitHub
parent 6e327184bd
commit ffb53405fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 85 deletions

View file

@ -134,10 +134,10 @@ auto_filenames = {
"lib/renderer/callbacks-registry.js", "lib/renderer/callbacks-registry.js",
"lib/renderer/chrome-api.ts", "lib/renderer/chrome-api.ts",
"lib/renderer/content-scripts-injector.ts", "lib/renderer/content-scripts-injector.ts",
"lib/renderer/extensions/event.js", "lib/renderer/extensions/event.ts",
"lib/renderer/extensions/i18n.js", "lib/renderer/extensions/i18n.ts",
"lib/renderer/extensions/storage.js", "lib/renderer/extensions/storage.ts",
"lib/renderer/extensions/web-navigation.js", "lib/renderer/extensions/web-navigation.ts",
"lib/renderer/inspector.ts", "lib/renderer/inspector.ts",
"lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal-utils.ts",
"lib/renderer/ipc-renderer-internal.ts", "lib/renderer/ipc-renderer-internal.ts",
@ -174,10 +174,10 @@ auto_filenames = {
"lib/common/error-utils.js", "lib/common/error-utils.js",
"lib/content_script/init.js", "lib/content_script/init.js",
"lib/renderer/chrome-api.ts", "lib/renderer/chrome-api.ts",
"lib/renderer/extensions/event.js", "lib/renderer/extensions/event.ts",
"lib/renderer/extensions/i18n.js", "lib/renderer/extensions/i18n.ts",
"lib/renderer/extensions/storage.js", "lib/renderer/extensions/storage.ts",
"lib/renderer/extensions/web-navigation.js", "lib/renderer/extensions/web-navigation.ts",
"lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal-utils.ts",
"lib/renderer/ipc-renderer-internal.ts", "lib/renderer/ipc-renderer-internal.ts",
"lib/renderer/window-setup.ts", "lib/renderer/window-setup.ts",
@ -294,10 +294,10 @@ auto_filenames = {
"lib/renderer/callbacks-registry.js", "lib/renderer/callbacks-registry.js",
"lib/renderer/chrome-api.ts", "lib/renderer/chrome-api.ts",
"lib/renderer/content-scripts-injector.ts", "lib/renderer/content-scripts-injector.ts",
"lib/renderer/extensions/event.js", "lib/renderer/extensions/event.ts",
"lib/renderer/extensions/i18n.js", "lib/renderer/extensions/i18n.ts",
"lib/renderer/extensions/storage.js", "lib/renderer/extensions/storage.ts",
"lib/renderer/extensions/web-navigation.js", "lib/renderer/extensions/web-navigation.ts",
"lib/renderer/init.ts", "lib/renderer/init.ts",
"lib/renderer/inspector.ts", "lib/renderer/inspector.ts",
"lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal-utils.ts",

View file

@ -2,8 +2,7 @@ import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-in
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils' import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
import * as url from 'url' import * as url from 'url'
// Todo: Import once extensions have been turned into TypeScript import { Event } from '@electron/internal/renderer/extensions/event'
const Event = require('@electron/internal/renderer/extensions/event')
class Tab { class Tab {
public id: number public id: number

View file

@ -1,26 +1,20 @@
'use strict' export class Event {
private listeners: Function[] = []
class Event { addListener (callback: Function) {
constructor () {
this.listeners = []
}
addListener (callback) {
this.listeners.push(callback) this.listeners.push(callback)
} }
removeListener (callback) { removeListener (callback: Function) {
const index = this.listeners.indexOf(callback) const index = this.listeners.indexOf(callback)
if (index !== -1) { if (index !== -1) {
this.listeners.splice(index, 1) this.listeners.splice(index, 1)
} }
} }
emit (...args) { emit (...args: any[]) {
for (const listener of this.listeners) { for (const listener of this.listeners) {
listener(...args) listener(...args)
} }
} }
} }
module.exports = Event

View file

@ -1,41 +1,41 @@
'use strict'
// Implementation of chrome.i18n.getMessage // Implementation of chrome.i18n.getMessage
// https://developer.chrome.com/extensions/i18n#method-getMessage // https://developer.chrome.com/extensions/i18n#method-getMessage
// //
// Does not implement predefined messages: // Does not implement predefined messages:
// https://developer.chrome.com/extensions/i18n#overview-predefined // https://developer.chrome.com/extensions/i18n#overview-predefined
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
const getMessages = (extensionId) => { interface Placeholder {
content: string;
example?: string;
}
const getMessages = (extensionId: number) => {
try { try {
const data = ipcRendererUtils.invokeSync('CHROME_GET_MESSAGES', extensionId) const data = ipcRendererUtils.invokeSync<string>('CHROME_GET_MESSAGES', extensionId)
return JSON.parse(data) || {} return JSON.parse(data) || {}
} catch { } catch {
return {} return {}
} }
} }
const replaceNumberedSubstitutions = (message, substitutions) => { const replaceNumberedSubstitutions = (message: string, substitutions: string[]) => {
return message.replace(/\$(\d+)/, (_, number) => { return message.replace(/\$(\d+)/, (_, number) => {
const index = parseInt(number, 10) - 1 const index = parseInt(number, 10) - 1
return substitutions[index] || '' return substitutions[index] || ''
}) })
} }
const replacePlaceholders = (message, placeholders, substitutions) => { const replacePlaceholders = (message: string, placeholders: Record<string, Placeholder>, substitutions: string[] | string) => {
if (typeof substitutions === 'string') { if (typeof substitutions === 'string') substitutions = [substitutions]
substitutions = [substitutions] if (!Array.isArray(substitutions)) substitutions = []
}
if (!Array.isArray(substitutions)) {
substitutions = []
}
if (placeholders) { if (placeholders) {
Object.keys(placeholders).forEach((name) => { Object.keys(placeholders).forEach((name: string) => {
let { content } = placeholders[name] let { content } = placeholders[name]
content = replaceNumberedSubstitutions(content, substitutions) const substitutionsArray = Array.isArray(substitutions) ? substitutions : []
content = replaceNumberedSubstitutions(content, substitutionsArray)
message = message.replace(new RegExp(`\\$${name}\\$`, 'gi'), content) message = message.replace(new RegExp(`\\$${name}\\$`, 'gi'), content)
}) })
} }
@ -43,7 +43,7 @@ const replacePlaceholders = (message, placeholders, substitutions) => {
return replaceNumberedSubstitutions(message, substitutions) return replaceNumberedSubstitutions(message, substitutions)
} }
const getMessage = (extensionId, messageName, substitutions) => { const getMessage = (extensionId: number, messageName: string, substitutions: string[]) => {
const messages = getMessages(extensionId) const messages = getMessages(extensionId)
if (messages.hasOwnProperty(messageName)) { if (messages.hasOwnProperty(messageName)) {
const { message, placeholders } = messages[messageName] const { message, placeholders } = messages[messageName]
@ -51,9 +51,9 @@ const getMessage = (extensionId, messageName, substitutions) => {
} }
} }
exports.setup = (extensionId) => { exports.setup = (extensionId: number) => {
return { return {
getMessage (messageName, substitutions) { getMessage (messageName: string, substitutions: string[]) {
return getMessage(extensionId, messageName, substitutions) return getMessage(extensionId, messageName, substitutions)
} }
} }

View file

@ -1,11 +1,9 @@
'use strict' import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') const getStorage = (storageType: string, extensionId: number, callback: Function) => {
const getStorage = (storageType, extensionId, callback) => {
if (typeof callback !== 'function') throw new TypeError('No callback provided') if (typeof callback !== 'function') throw new TypeError('No callback provided')
ipcRendererUtils.invoke('CHROME_STORAGE_READ', storageType, extensionId) ipcRendererUtils.invoke<string>('CHROME_STORAGE_READ', storageType, extensionId)
.then(data => { .then(data => {
if (data !== null) { if (data !== null) {
callback(JSON.parse(data)) callback(JSON.parse(data))
@ -17,7 +15,7 @@ const getStorage = (storageType, extensionId, callback) => {
}) })
} }
const setStorage = (storageType, extensionId, storage, callback) => { const setStorage = (storageType: string, extensionId: number, storage: Record<string, any>, callback: Function) => {
const json = JSON.stringify(storage) const json = JSON.stringify(storage)
ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json) ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json)
.then(() => { .then(() => {
@ -25,13 +23,13 @@ const setStorage = (storageType, extensionId, storage, callback) => {
}) })
} }
const getStorageManager = (storageType, extensionId) => { const getStorageManager = (storageType: string, extensionId: number) => {
return { return {
get (keys, callback) { get (keys: string[], callback: Function) {
getStorage(storageType, extensionId, storage => { getStorage(storageType, extensionId, (storage: Record<string, any>) => {
if (keys == null) return callback(storage) if (keys == null) return callback(storage)
let defaults = {} let defaults: Record<string, any> = {}
switch (typeof keys) { switch (typeof keys) {
case 'string': case 'string':
keys = [keys] keys = [keys]
@ -48,8 +46,8 @@ const getStorageManager = (storageType, extensionId) => {
// eslint-disable-next-line standard/no-callback-literal // eslint-disable-next-line standard/no-callback-literal
if (keys.length === 0) return callback({}) if (keys.length === 0) return callback({})
const items = {} const items: Record<string, any> = {}
keys.forEach(function (key) { keys.forEach((key: string) => {
let value = storage[key] let value = storage[key]
if (value == null) value = defaults[key] if (value == null) value = defaults[key]
items[key] = value items[key] = value
@ -58,22 +56,17 @@ const getStorageManager = (storageType, extensionId) => {
}) })
}, },
set (items, callback) { set (items: Record<string, any>, callback: Function) {
getStorage(storageType, extensionId, storage => { getStorage(storageType, extensionId, (storage: Record<string, any>) => {
Object.keys(items).forEach(function (name) { Object.keys(items).forEach(name => { storage[name] = items[name] })
storage[name] = items[name]
})
setStorage(storageType, extensionId, storage, callback) setStorage(storageType, extensionId, storage, callback)
}) })
}, },
remove (keys, callback) { remove (keys: string[], callback: Function) {
getStorage(storageType, extensionId, storage => { getStorage(storageType, extensionId, (storage: Record<string, any>) => {
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) keys = [keys]
keys = [keys] keys.forEach((key: string) => {
}
keys.forEach(function (key) {
delete storage[key] delete storage[key]
}) })
@ -81,15 +74,13 @@ const getStorageManager = (storageType, extensionId) => {
}) })
}, },
clear (callback) { clear (callback: Function) {
setStorage(storageType, extensionId, {}, callback) setStorage(storageType, extensionId, {}, callback)
} }
} }
} }
module.exports = { export const setup = (extensionId: number) => ({
setup: extensionId => ({ sync: getStorageManager('sync', extensionId),
sync: getStorageManager('sync', extensionId), local: getStorageManager('local', extensionId)
local: getStorageManager('local', extensionId) })
})
}

View file

@ -1,23 +1,20 @@
'use strict' import { Event } from '@electron/internal/renderer/extensions/event'
import { IpcMainEvent } from 'electron'
const Event = require('@electron/internal/renderer/extensions/event')
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
class WebNavigation { class WebNavigation {
constructor () { private onBeforeNavigate = new Event()
this.onBeforeNavigate = new Event() private onCompleted = new Event()
this.onCompleted = new Event()
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event, details) => { constructor () {
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event: IpcMainEvent, details: any) => {
this.onBeforeNavigate.emit(details) this.onBeforeNavigate.emit(details)
}) })
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event, details) => { ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event: IpcMainEvent, details: any) => {
this.onCompleted.emit(details) this.onCompleted.emit(details)
}) })
} }
} }
exports.setup = () => { export const setup = () => new WebNavigation()
return new WebNavigation()
}