2023-07-21 19:13:51 +08:00
|
|
|
/**
|
|
|
|
* {注入的js}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2023/07/21 17:38:11
|
|
|
|
*/
|
2023-07-25 18:53:52 +08:00
|
|
|
const MIME_TYPES = {
|
|
|
|
html: 'text/html',
|
|
|
|
json: 'application/json',
|
|
|
|
js: 'application/javascript',
|
|
|
|
htm: 'text/html',
|
|
|
|
txt: 'text/plain',
|
|
|
|
css: 'text/css',
|
|
|
|
webp: 'image/webp',
|
|
|
|
jpg: 'image/jpg',
|
|
|
|
jpeg: 'image/jpeg',
|
|
|
|
png: 'image/png',
|
|
|
|
gif: 'image/gif',
|
|
|
|
svg: 'image/svg+xml',
|
|
|
|
ico: 'image/ico',
|
|
|
|
mp3: 'audio/mpeg',
|
|
|
|
ogg: 'audio/ogg',
|
|
|
|
m4a: 'audio/m4a',
|
|
|
|
amr: 'audio/amr',
|
|
|
|
mp4: 'video/mp4',
|
|
|
|
webm: 'video/webm',
|
|
|
|
wasm: 'application/wasm',
|
|
|
|
asm: 'application/asm',
|
|
|
|
zip: 'application/zip',
|
|
|
|
'7z': 'application/x-7z-compressed',
|
|
|
|
eot: 'application/vnd.ms-fontobject',
|
|
|
|
ttf: 'font/ttf',
|
|
|
|
otf: 'font/otf',
|
|
|
|
woff: 'font/woff',
|
|
|
|
woff2: 'font/woff2',
|
|
|
|
xls: 'application/vnd.ms-excel',
|
|
|
|
doc: 'application/msword',
|
|
|
|
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
|
|
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
|
|
}
|
2023-07-27 20:25:43 +08:00
|
|
|
const KEYS_MAP = {
|
|
|
|
shift: '<Shift>',
|
|
|
|
ctrl: '<Ctrl>',
|
|
|
|
alt: '<Alt>',
|
|
|
|
super: '<Super>'
|
|
|
|
}
|
2023-07-21 19:13:51 +08:00
|
|
|
|
2023-07-25 12:29:31 +08:00
|
|
|
function defer() {
|
|
|
|
let obj = {}
|
|
|
|
obj.promise = new Promise((resolve, reject) => {
|
|
|
|
obj.resolve = resolve
|
|
|
|
obj.reject = reject
|
|
|
|
})
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
function rand(prefix = 'cb_') {
|
|
|
|
return prefix + Math.random().toString().slice(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handler(event, data = {}, once = true) {
|
|
|
|
let _ = defer()
|
2023-07-25 18:53:52 +08:00
|
|
|
let callback
|
|
|
|
|
|
|
|
if (typeof once === 'boolean') {
|
|
|
|
callback = rand()
|
|
|
|
native[once ? '$once' : '$on'](callback, _.resolve)
|
|
|
|
} else {
|
|
|
|
_.resolve(true)
|
|
|
|
}
|
2023-07-25 12:29:31 +08:00
|
|
|
window.webkit.messageHandlers.app.postMessage({
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
callback
|
|
|
|
})
|
|
|
|
return _.promise
|
|
|
|
}
|
|
|
|
|
2023-07-28 19:38:24 +08:00
|
|
|
function base64(str = '') {
|
|
|
|
return btoa(str).replace(/[+=\/]/g, '')
|
|
|
|
}
|
|
|
|
|
2023-07-25 18:53:52 +08:00
|
|
|
class NativeImage {
|
|
|
|
#origin
|
|
|
|
|
|
|
|
constructor(obj) {
|
|
|
|
this.#origin = obj
|
|
|
|
this.width = obj.width
|
|
|
|
this.height = obj.height
|
|
|
|
this.type = MIME_TYPES[obj.filepath.split('.').pop()]
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:19:19 +08:00
|
|
|
toJSON() {
|
2023-07-25 18:53:52 +08:00
|
|
|
return this.#origin
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:19:19 +08:00
|
|
|
export(type, base64) {
|
2023-07-25 18:53:52 +08:00
|
|
|
let _ = defer()
|
|
|
|
let canvas = document.createElement('canvas')
|
|
|
|
canvas.width = this.width
|
|
|
|
canvas.height = this.height
|
|
|
|
let ctx = canvas.getContext('2d')
|
|
|
|
let imgData = ctx.getImageData(0, 0, this.width, this.height)
|
|
|
|
let data = imgData.data
|
|
|
|
|
|
|
|
for (let i = 0; i < this.#origin.bytes.length; i += 4) {
|
|
|
|
imgData.data[i] = this.#origin.bytes[i]
|
|
|
|
imgData.data[i + 1] = this.#origin.bytes[i + 1]
|
|
|
|
imgData.data[i + 2] = this.#origin.bytes[i + 2]
|
|
|
|
imgData.data[i + 3] = this.#origin.bytes[i + 3]
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.putImageData(imgData, 0, 0)
|
|
|
|
|
2023-07-31 16:19:19 +08:00
|
|
|
if (base64) {
|
|
|
|
return canvas.toDataURL(type || this.type, 1)
|
|
|
|
} else {
|
|
|
|
canvas.toBlob(_.resolve, type || this.type, 1)
|
|
|
|
return _.promise
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toPNG() {
|
|
|
|
return this.export('image/png')
|
|
|
|
}
|
|
|
|
|
|
|
|
toJPEG() {
|
|
|
|
return this.export('image/jpeg')
|
|
|
|
}
|
|
|
|
|
|
|
|
toDataURL(type) {
|
|
|
|
return this.export(type, true)
|
2023-07-25 18:53:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:05:01 +08:00
|
|
|
class EventEmitter {
|
2023-07-21 19:13:51 +08:00
|
|
|
//
|
2023-07-24 15:05:01 +08:00
|
|
|
__events__ = Object.create(null)
|
2023-07-21 19:13:51 +08:00
|
|
|
|
|
|
|
$on(name, fn) {
|
|
|
|
if (this.__events__[name]) {
|
|
|
|
this.__events__[name].push(fn)
|
|
|
|
} else {
|
|
|
|
this.__events__[name] = [fn]
|
|
|
|
}
|
2023-07-24 15:05:01 +08:00
|
|
|
}
|
2023-07-21 19:13:51 +08:00
|
|
|
|
|
|
|
$once(name, fn) {
|
|
|
|
fn.__once__ = true
|
|
|
|
this.$on(name, fn)
|
2023-07-24 15:05:01 +08:00
|
|
|
}
|
2023-07-21 19:13:51 +08:00
|
|
|
|
|
|
|
$off(name, fn) {
|
|
|
|
if (this.__events__[name]) {
|
|
|
|
if (fn) {
|
|
|
|
this.__events__[name] = this.__events__[name].filter(it => it !== fn)
|
|
|
|
} else {
|
|
|
|
this.__events__[name] = []
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 15:05:01 +08:00
|
|
|
}
|
2023-07-21 19:13:51 +08:00
|
|
|
|
|
|
|
$emit(name, ...args) {
|
|
|
|
if (this.__events__[name]) {
|
|
|
|
for (let fn of this.__events__[name]) {
|
|
|
|
try {
|
|
|
|
fn.apply(this, args)
|
|
|
|
if (fn.__once__) {
|
|
|
|
this.$off(name, fn)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 15:05:01 +08:00
|
|
|
}
|
2023-07-21 19:13:51 +08:00
|
|
|
|
|
|
|
$destroy() {
|
|
|
|
this.__events__ = Object.create(null)
|
|
|
|
}
|
2023-07-24 15:05:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
window.native = new EventEmitter()
|
2023-07-25 12:29:31 +08:00
|
|
|
|
|
|
|
Object.assign(native, {
|
2023-07-25 18:53:52 +08:00
|
|
|
quit() {
|
2023-07-31 12:12:29 +08:00
|
|
|
return handler('quit', {}, null)
|
2023-07-25 18:53:52 +08:00
|
|
|
},
|
2023-07-25 12:29:31 +08:00
|
|
|
fs: {
|
2023-08-01 15:25:13 +08:00
|
|
|
read(filepath, mode = 'r') {
|
|
|
|
return handler('fs', { action: 'read', mode, filepath }).then(r =>
|
|
|
|
mode.includes('b') ? new Uint8Array(r) : r
|
|
|
|
)
|
2023-07-31 17:09:27 +08:00
|
|
|
},
|
2023-08-01 15:25:13 +08:00
|
|
|
write(filepath, content = '', mode = 'w') {
|
2023-07-31 17:09:27 +08:00
|
|
|
return handler('fs', {
|
|
|
|
action: 'write',
|
2023-08-01 15:25:13 +08:00
|
|
|
append: false,
|
2023-07-31 17:09:27 +08:00
|
|
|
filepath,
|
2023-08-01 15:25:13 +08:00
|
|
|
content
|
|
|
|
})
|
|
|
|
},
|
|
|
|
append(filepath, content = '', mode = 'w') {
|
|
|
|
return handler('fs', {
|
|
|
|
action: 'write',
|
|
|
|
append: true,
|
|
|
|
filepath,
|
|
|
|
content
|
2023-07-31 17:09:27 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
exists(filepath) {
|
|
|
|
return handler('fs', { action: 'exists', filepath })
|
|
|
|
},
|
2023-08-01 15:25:13 +08:00
|
|
|
list(filepath) {
|
|
|
|
return handler('fs', { action: 'list', filepath })
|
|
|
|
},
|
2023-07-31 17:09:27 +08:00
|
|
|
isfile(filepath) {
|
|
|
|
return handler('fs', { action: 'isfile', filepath })
|
|
|
|
},
|
|
|
|
isdir(filepath) {
|
|
|
|
return handler('fs', { action: 'isdir', filepath })
|
|
|
|
},
|
|
|
|
remove(filepath) {
|
|
|
|
return handler('fs', { action: 'remove', filepath })
|
|
|
|
},
|
|
|
|
rename(filepath, target) {
|
|
|
|
return handler('fs', { action: 'rename', filepath, target })
|
|
|
|
},
|
|
|
|
copy(filepath, target) {
|
|
|
|
return handler('fs', { action: 'copy', filepath, target })
|
|
|
|
}
|
2023-07-25 12:29:31 +08:00
|
|
|
},
|
2023-07-25 18:53:52 +08:00
|
|
|
image(filepath) {
|
|
|
|
return handler('image', { value: filepath }).then(r => new NativeImage(r))
|
|
|
|
},
|
|
|
|
clipboard: {
|
|
|
|
readText() {
|
|
|
|
return handler('clipboard', { action: 'wait_for_text' })
|
|
|
|
},
|
|
|
|
writeText(value) {
|
|
|
|
return handler('clipboard', { action: 'set_text', value }, null)
|
|
|
|
},
|
|
|
|
readImage() {
|
2023-07-28 17:51:37 +08:00
|
|
|
return handler('clipboard', { action: 'wait_for_image' }).then(r =>
|
|
|
|
r ? new NativeImage(r) : r
|
|
|
|
)
|
2023-07-25 18:53:52 +08:00
|
|
|
},
|
|
|
|
writeImage(value) {
|
2023-07-31 16:19:19 +08:00
|
|
|
// 暂不知原因, postMessage传给Gtk后, JSON.stringify()并未读取toJSON的结果
|
2023-07-25 18:53:52 +08:00
|
|
|
if (typeof value === 'object') {
|
2023-07-31 16:19:19 +08:00
|
|
|
value = value.toJSON()
|
2023-07-25 18:53:52 +08:00
|
|
|
}
|
|
|
|
return handler('clipboard', { action: 'set_image', value }, null)
|
|
|
|
},
|
|
|
|
clear() {
|
|
|
|
return handler('clipboard', { action: 'clear' })
|
|
|
|
}
|
|
|
|
},
|
2023-07-26 17:55:29 +08:00
|
|
|
screen: {
|
|
|
|
getAllDisplays() {
|
|
|
|
return handler('monitor', { action: 'get-all' })
|
|
|
|
},
|
|
|
|
getPrimaryDisplay() {
|
|
|
|
return handler('monitor', { action: 'get-primary' })
|
|
|
|
}
|
|
|
|
},
|
2023-07-27 20:25:43 +08:00
|
|
|
globalShortcut: {
|
|
|
|
get enabled() {
|
|
|
|
return handler('keybinder', { action: 'supported' })
|
|
|
|
},
|
|
|
|
register(keyMap, callback) {
|
2023-07-28 19:38:24 +08:00
|
|
|
let shortcut_callback = base64(keyMap)
|
2023-07-27 20:25:43 +08:00
|
|
|
native.$off(shortcut_callback)
|
|
|
|
native.$on(shortcut_callback, callback)
|
|
|
|
return handler('keybinder', {
|
|
|
|
action: 'register',
|
|
|
|
value: keyMap,
|
|
|
|
shortcut_callback
|
|
|
|
})
|
|
|
|
},
|
2023-07-28 19:38:24 +08:00
|
|
|
unregister(keyMap) {
|
|
|
|
let shortcut_callback = base64(keyMap)
|
|
|
|
native.$off(shortcut_callback)
|
|
|
|
return handler('keybinder', { action: 'unregister', value: keyMap })
|
2023-07-27 20:25:43 +08:00
|
|
|
},
|
2023-07-28 19:38:24 +08:00
|
|
|
unregisterAll(keyMaps) {
|
|
|
|
for (let it of keyMaps) {
|
|
|
|
this.unregister(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tray: {
|
|
|
|
create() {
|
2023-07-27 20:25:43 +08:00
|
|
|
//
|
|
|
|
},
|
2023-07-28 19:38:24 +08:00
|
|
|
remove() {
|
2023-07-27 20:25:43 +08:00
|
|
|
//
|
|
|
|
},
|
2023-07-28 19:38:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置普通状态的tray图标, 只需要传名称, 自动会去当前主题下去找
|
|
|
|
*/
|
|
|
|
set_icon(name) {
|
|
|
|
return handler('tray', { action: 'set_icon', value: name })
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置警示图标, 同上
|
|
|
|
*/
|
|
|
|
set_attention_icon(name) {
|
|
|
|
return handler('tray', { action: 'set_attention_icon', value: name })
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
set_title(title) {
|
|
|
|
return handler('tray', { action: 'set_title', value: title })
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改tray图标状态
|
|
|
|
* @param status <Number> 0: 隐藏, 1: 显示, 2: 重要(对应上面的attention_icon)
|
|
|
|
*/
|
|
|
|
set_status(status) {
|
|
|
|
return handler('tray', { action: 'set_status', value: status })
|
2023-07-27 20:25:43 +08:00
|
|
|
}
|
|
|
|
},
|
2023-07-31 12:12:29 +08:00
|
|
|
window: {
|
|
|
|
isVisible() {
|
|
|
|
return handler('window', { action: 'is_visible' })
|
|
|
|
},
|
|
|
|
toggleVisible() {
|
|
|
|
handler('window', { action: 'toggle_visible' }, null)
|
|
|
|
},
|
|
|
|
hide() {
|
|
|
|
handler('window', { action: 'hide' }, null)
|
|
|
|
},
|
|
|
|
show() {
|
|
|
|
handler('window', { action: 'show' }, null)
|
|
|
|
},
|
|
|
|
fullscreen() {
|
|
|
|
handler('window', { action: 'fullscreen' }, null)
|
|
|
|
},
|
|
|
|
unfullscreen() {
|
|
|
|
handler('window', { action: 'unfullscreen' }, null)
|
|
|
|
},
|
|
|
|
maximize() {
|
|
|
|
handler('window', { action: 'maximize' }, null)
|
|
|
|
},
|
|
|
|
unmaximize() {
|
|
|
|
handler('window', { action: 'unmaximize' }, null)
|
|
|
|
},
|
|
|
|
setTitle(title = '') {
|
|
|
|
handler('window', { action: 'set_title', value: title }, null)
|
|
|
|
},
|
|
|
|
resize(width = 0, height = 0) {
|
|
|
|
handler('window', { action: 'resize', value: { width, height } }, null)
|
|
|
|
},
|
|
|
|
move(x = 0, y = 0) {
|
|
|
|
handler('window', { action: 'resize', value: { x, y } }, null)
|
|
|
|
},
|
|
|
|
setOpacity(opacity = 1) {
|
|
|
|
handler('window', { action: 'set_opacity', value: opacity }, null)
|
|
|
|
},
|
|
|
|
alwayOnTop(setting = true) {
|
|
|
|
handler('window', { action: 'set_keep_above', value: setting }, null)
|
|
|
|
},
|
|
|
|
alwayOnBotttom(setting = true) {
|
|
|
|
handler('window', { action: 'set_keep_below', value: setting }, null)
|
|
|
|
}
|
|
|
|
},
|
2023-08-01 14:20:51 +08:00
|
|
|
notify({ title, summary, icon, progress = 0, urgency = 0, callback }) {
|
|
|
|
let eventName
|
|
|
|
if (callback) {
|
|
|
|
eventName = rand()
|
|
|
|
native.$once(eventName, callback)
|
|
|
|
}
|
|
|
|
handler(
|
|
|
|
'notify',
|
|
|
|
{ title, summary, icon, progress, urgency, callback: eventName },
|
|
|
|
null
|
|
|
|
)
|
2023-07-31 19:28:17 +08:00
|
|
|
},
|
2023-08-01 18:40:52 +08:00
|
|
|
|
|
|
|
md5(value = '') {
|
|
|
|
return handler('md5', { value })
|
|
|
|
},
|
|
|
|
|
2023-07-25 12:29:31 +08:00
|
|
|
handler
|
|
|
|
})
|