UNPKG

electron-shortcut-capture-tc

Version:

**electron-shortcut-capture 是一个用在 electron 上使用的截图工具。**

18 lines (16 loc) 20.8 kB
module.exports = function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var i = t[r] = { i: r, l: !1, exports: {} }; return e[r].call(i.exports, i, i.exports, n), i.l = !0, i.exports } return n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { enumerable: !0, get: r }) }, n.r = function (e) { "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e, "__esModule", { value: !0 }) }, n.t = function (e, t) { if (1 & t && (e = n(e)), 8 & t) return e; if (4 & t && "object" == typeof e && e && e.__esModule) return e; var r = Object.create(null); if (n.r(r), Object.defineProperty(r, "default", { enumerable: !0, value: e }), 2 & t && "string" != typeof e) for (var i in e) n.d(r, i, function (t) { return e[t] }.bind(null, i)); return r }, n.n = function (e) { var t = e && e.__esModule ? function () { return e.default } : function () { return e }; return n.d(t, "a", t), t }, n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) }, n.p = "", n(n.s = 0) }([function (e, t, n) { "use strict"; Object.defineProperty(t, "__esModule", { value: !0 }); var r = n(1), i = n(2), o = n(3), s = n(4), u = n(5), c = n(6), a = function () { function e(e) { var t = this; this.captureWins = [], this.multiScreen = !1, this.key = "", this.displays = [], this.shortcuting = !1, this.downloadFileprefix = "", this.screenInfo = {}, this.isDownloading = !1, this.loadedPageDisplayIds = [], this.onClipboard = null, this.onHide = null, this.onShow = null, this.onShowByKey = null, this.getLogger = null, this.getCurrentFocusDisplay = function () { var e = i.screen.getCursorScreenPoint(); return { mousePoint: e, currDisplay: i.screen.getDisplayNearestPoint(e) } }, this.setScreenInfo = function (e) { t.getLogger("setScreenInfo-display" + s.inspect(e)), t.screenInfo[e.id] = {}, t.screenInfo.cutWidth = t.screenInfo.cutWidth && t.screenInfo.cutWidth > e.size.width * e.scaleFactor ? t.screenInfo.cutWidth : e.size.width * e.scaleFactor, t.screenInfo.cutHeight = t.screenInfo.cutHeight && t.screenInfo.cutHeight > e.size.height * e.scaleFactor ? t.screenInfo.cutHeight : e.size.height * e.scaleFactor }, this.getScreenSources = function (e, t) { return new Promise((function (n) { var r = process.electronBinding("desktop_capturer").createDesktopCapturer(), i = new o.EventEmitter; i.once("finished", (function (e, t) { r && (r.emit = null, r = null), n(t) })), r.emit = i.emit.bind(i), r.startHandling(!1, !0, { width: Math.ceil(e), height: Math.ceil(t) }, !0) })) }, this.listenEsc = function () { i.globalShortcut.register("esc", (function () { t.hide() })) }, this.unListenEsc = function () { i.globalShortcut.unregister("esc") }, this.bindKey = function () { t.key && i.globalShortcut.register(t.key, (function () { return r.__awaiter(t, void 0, void 0, (function () { return r.__generator(this, (function (e) { switch (e.label) { case 0: return "function" != typeof this.onShowByKey ? [3, 2] : [4, this.onShowByKey()]; case 1: e.sent(), e.label = 2; case 2: return this.show(), [2] } })) })) })) }, this.updateBindKey = function (e) { if (e) try { t.key && i.globalShortcut.unregister(t.key), t.key = e, i.globalShortcut.register(e, (function () { return r.__awaiter(t, void 0, void 0, (function () { return r.__generator(this, (function (e) { switch (e.label) { case 0: return "function" != typeof this.onShowByKey ? [3, 2] : [4, this.onShowByKey()]; case 1: e.sent(), e.label = 2; case 2: return this.show(), [2] } })) })) })) } catch (e) { t.key = "" } else try { i.globalShortcut.unregister(t.key), t.key = e } catch (e) { t.key = "" } }, this.listenDisplayNumChange = function () { i.screen.on("display-metrics-changed", (function () { t.reInit() })), i.screen.on("display-added", (function () { t.reInit() })), i.screen.on("display-removed", (function () { t.reInit() })) }, this.reInit = function () { t.getLogger("重新初始化"), t.screenInfo = {}, t.loadedPageDisplayIds = [], t.captureWins.forEach((function (e) { e.close() })), t.initWin() }, this.getLoadedPageDisplayId = function () { t.getLogger("绑定获取加载完的显示器的id--------"), i.ipcMain.on(c.events.loadedPageDisplayId, (function (e, n) { t.loadedPageDisplayIds.includes(n) || (t.loadedPageDisplayIds.push(n), t.getLogger("加载完的显示器id:" + n), t.loadedPageDisplayIds.length === t.captureWins.length && t.captureWins.forEach((function (e) { e.hide() }))) })) }, this.shortCutScreenIsOpened = function () { return t.multiScreen ? t.captureWins.every((function (e) { return e.isVisible() })) : t.captureWins.some((function (e) { return e.isVisible() })) }, this.getSourcePng = function (e, t, n) { return e.thumbnail.resize({ width: t, height: n }).toJPEG(100) }, this.multiScreen = !!e && !!e.multiScreen, this.downloadFileprefix = e && e.downloadFileprefix || "", this.onClipboard = e ? e.onClipboard : null, this.key = e ? e.key : "", this.onHide = e ? e.onHide : null, this.onShow = e ? e.onShow : null, this.onShowByKey = e ? e.onShowByKey : null, this.getLogger = e && e.getLogger ? function (t) { e.getLogger(t) } : function (e) { console.log(e) }, this.initWin(), this.bindHide(), this.bindClipboard(), this.bindDownload(), this.listenCapturingDisplayId(), this.bindKey(), this.listenDisplayNumChange(), this.getLoadedPageDisplayId() } return e.prototype.initWin = function () { return r.__awaiter(this, void 0, void 0, (function () { var t = this; return r.__generator(this, (function (n) { return this.displays = i.screen.getAllDisplays(), this.getLogger("初始化截图"), this.captureWins = this.displays.map((function (e) { t.setScreenInfo(e); var n = new i.BrowserWindow(u.default(e)); return n.displayId = e.id, n })), this.getLogger("设备数量:" + this.captureWins.length), this.captureWins.forEach((function (t) { t.loadURL(e.URL) })), [2] })) })) }, e.prototype.show = function () { return r.__awaiter(this, void 0, void 0, (function () { var e, t, n, i, o, u, a, l, f, d, h, p, g, y, b, w, v, _, S, m, I, P = this; return r.__generator(this, (function (r) { switch (r.label) { case 0: return r.trys.push([0, 2, , 3]), this.shortcuting && this.shortCutScreenIsOpened() ? [2, this.getLogger("正在截图")] : this.isDownloading ? [2, this.getLogger("正在执行下载操作")] : this.captureWins.length ? this.loadedPageDisplayIds.length !== this.captureWins.length ? [2, this.getLogger("页面没完全加载")] : (this.shortcuting = !0, this.getLogger("开始截图"), "function" == typeof this.onShow && this.onShow(), e = this.screenInfo.cutWidth, t = this.screenInfo.cutHeight, this.getLogger("screenInfo:" + s.inspect(this.screenInfo)), this.getLogger("截图宽高:" + e + " X " + t), [4, this.getScreenSources(e, t)]) : [2, this.getLogger("当前没有窗口")]; case 1: if (n = r.sent(), this.getLogger("截图sources:" + s.inspect(n, { depth: null })), i = n.some((function (e) { return !e.display_id })), this.getLogger(i ? "存在DisplayId是否为空问题" : "不存在DisplayId是否为空问题"), i && (this.multiScreen = !1), o = this.getCurrentFocusDisplay(), u = o.currDisplay, a = o.mousePoint, l = a.x - u.bounds.x, f = a.y - u.bounds.y, d = function (e) { var t = e.win, n = e.source, r = e.width, i = e.height, o = e.displayId, s = e.scaleFactor; t.webContents.send(c.events.screenSourcesToPng, { toPngSource: P.getSourcePng(n, r * s, i * s), width: r, height: i, mouseX: l, mouseY: f, displayId: o, scaleFactor: s }), t.setVisibleOnAllWorkspaces(!0), t.setAlwaysOnTop(!0, "screen-saver"), t.setBackgroundColor("#00000000"), t.show() }, this.multiScreen) for (h = 0; h < n.length; h++)v = this.captureWins[h], p = this.displays[h], y = n[h], g = v.getBounds(), S = g.width, m = g.height, this.getLogger("noticeToRenderer:" + s.inspect({ win: v, source: y, width: S, height: m, displayId: p.id, scaleFactor: p.scaleFactor }, { depth: null })), d({ win: v, source: y, width: S, height: m, displayId: p.id, scaleFactor: p.scaleFactor }); else y = void 0, b = u.id.toString(), (y = n.filter((function (e) { return e.display_id === b }))[0]) || (w = Object.keys(this.screenInfo).findIndex((function (e) { return e === b })), y = n[w]), v = this.captureWins.filter((function (e) { return e.displayId === u.id }))[0], _ = v.getBounds(), S = _.width, m = _.height, this.getLogger("noticeToRenderer:" + s.inspect({ win: v, source: y, width: S, height: m, displayId: b, scaleFactor: u.scaleFactor }, { depth: null })), d({ win: v, source: y, width: S, height: m, displayId: b, scaleFactor: u.scaleFactor }); return this.listenEsc(), [3, 3]; case 2: return I = r.sent(), this.getLogger("打开截图失败:" + s.inspect(I, { depth: null })), [3, 3]; case 3: return [2] } })) })) }, e.prototype.bindHide = function () { var e = this; i.ipcMain.on(c.events.close, (function (t, n) { e.hide(n) })) }, e.prototype.hide = function (e) { var t = this; if (void 0 === e && (e = !0), !this.shortCutScreenIsOpened()) return this.getLogger("截图没完全打开"); this.loadedPageDisplayIds = [], this.shortcuting = !1, this.captureWins.forEach((function (n) { n.webContents.send(c.events.receiveCapturingDisplayId, 0), n.setVisibleOnAllWorkspaces(!1), e && n.webContents.send(c.events.close), t.isDownloading && n.hide() })), this.unListenEsc(), "function" == typeof this.onHide && this.onHide(), this.getLogger("关闭截图————————————————————————————————————————————————————") }, e.prototype.bindDownload = function () { var e = this; i.ipcMain.on(c.events.download, (function (t, r) { var o = r.dataURL; e.isDownloading = !0, e.hide(); var s = o.replace(/^data:image\/\w+;base64,/, ""), u = Buffer.from(s, "base64"), c = e.downloadFileprefix + (new Date).getTime() + ".png", a = i.dialog.showSaveDialogSync({ defaultPath: c }); if (!a) return e.isDownloading = !1, e.getLogger("取消下载"); try { n(7).writeFileSync(a, u) } catch (t) { e.getLogger("下载失败:" + t) } e.isDownloading = !1 })) }, e.prototype.bindClipboard = function () { var e = this; i.ipcMain.on(c.events.clipboard, (function (t, n) { var r = i.nativeImage.createFromDataURL(n); i.clipboard.writeImage(r), "function" == typeof e.onClipboard && e.onClipboard(n), e.hide() })) }, e.prototype.listenCapturingDisplayId = function () { var e = this; i.ipcMain.on(c.events.setCapturingDisplayId, (function (t, n) { e.captureWins.forEach((function (e) { e.webContents.send(c.events.receiveCapturingDisplayId, n) })) })) }, e.isWin = "darwin" !== n(8).platform(), e.URL = "file://" + n(9).join(__dirname, "../electron/app.html"), e }(); t.default = a }, function (e, t, n) { "use strict"; n.r(t), n.d(t, "__extends", (function () { return i })), n.d(t, "__assign", (function () { return o })), n.d(t, "__rest", (function () { return s })), n.d(t, "__decorate", (function () { return u })), n.d(t, "__param", (function () { return c })), n.d(t, "__metadata", (function () { return a })), n.d(t, "__awaiter", (function () { return l })), n.d(t, "__generator", (function () { return f })), n.d(t, "__exportStar", (function () { return d })), n.d(t, "__values", (function () { return h })), n.d(t, "__read", (function () { return p })), n.d(t, "__spread", (function () { return g })), n.d(t, "__spreadArrays", (function () { return y })), n.d(t, "__await", (function () { return b })), n.d(t, "__asyncGenerator", (function () { return w })), n.d(t, "__asyncDelegator", (function () { return v })), n.d(t, "__asyncValues", (function () { return _ })), n.d(t, "__makeTemplateObject", (function () { return S })), n.d(t, "__importStar", (function () { return m })), n.d(t, "__importDefault", (function () { return I })), n.d(t, "__classPrivateFieldGet", (function () { return P })), n.d(t, "__classPrivateFieldSet", (function () { return D })); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var r = function (e, t) { return (r = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (e, t) { e.__proto__ = t } || function (e, t) { for (var n in t) t.hasOwnProperty(n) && (e[n] = t[n]) })(e, t) }; function i(e, t) { function n() { this.constructor = e } r(e, t), e.prototype = null === t ? Object.create(t) : (n.prototype = t.prototype, new n) } var o = function () { return (o = Object.assign || function (e) { for (var t, n = 1, r = arguments.length; n < r; n++)for (var i in t = arguments[n]) Object.prototype.hasOwnProperty.call(t, i) && (e[i] = t[i]); return e }).apply(this, arguments) }; function s(e, t) { var n = {}; for (var r in e) Object.prototype.hasOwnProperty.call(e, r) && t.indexOf(r) < 0 && (n[r] = e[r]); if (null != e && "function" == typeof Object.getOwnPropertySymbols) { var i = 0; for (r = Object.getOwnPropertySymbols(e); i < r.length; i++)t.indexOf(r[i]) < 0 && Object.prototype.propertyIsEnumerable.call(e, r[i]) && (n[r[i]] = e[r[i]]) } return n } function u(e, t, n, r) { var i, o = arguments.length, s = o < 3 ? t : null === r ? r = Object.getOwnPropertyDescriptor(t, n) : r; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, n, r); else for (var u = e.length - 1; u >= 0; u--)(i = e[u]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, n, s) : i(t, n)) || s); return o > 3 && s && Object.defineProperty(t, n, s), s } function c(e, t) { return function (n, r) { t(n, r, e) } } function a(e, t) { if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(e, t) } function l(e, t, n, r) { return new (n || (n = Promise))((function (i, o) { function s(e) { try { c(r.next(e)) } catch (e) { o(e) } } function u(e) { try { c(r.throw(e)) } catch (e) { o(e) } } function c(e) { var t; e.done ? i(e.value) : (t = e.value, t instanceof n ? t : new n((function (e) { e(t) }))).then(s, u) } c((r = r.apply(e, t || [])).next()) })) } function f(e, t) { var n, r, i, o, s = { label: 0, sent: function () { if (1 & i[0]) throw i[1]; return i[1] }, trys: [], ops: [] }; return o = { next: u(0), throw: u(1), return: u(2) }, "function" == typeof Symbol && (o[Symbol.iterator] = function () { return this }), o; function u(o) { return function (u) { return function (o) { if (n) throw new TypeError("Generator is already executing."); for (; s;)try { if (n = 1, r && (i = 2 & o[0] ? r.return : o[0] ? r.throw || ((i = r.return) && i.call(r), 0) : r.next) && !(i = i.call(r, o[1])).done) return i; switch (r = 0, i && (o = [2 & o[0], i.value]), o[0]) { case 0: case 1: i = o; break; case 4: return s.label++, { value: o[1], done: !1 }; case 5: s.label++, r = o[1], o = [0]; continue; case 7: o = s.ops.pop(), s.trys.pop(); continue; default: if (!(i = (i = s.trys).length > 0 && i[i.length - 1]) && (6 === o[0] || 2 === o[0])) { s = 0; continue } if (3 === o[0] && (!i || o[1] > i[0] && o[1] < i[3])) { s.label = o[1]; break } if (6 === o[0] && s.label < i[1]) { s.label = i[1], i = o; break } if (i && s.label < i[2]) { s.label = i[2], s.ops.push(o); break } i[2] && s.ops.pop(), s.trys.pop(); continue }o = t.call(e, s) } catch (e) { o = [6, e], r = 0 } finally { n = i = 0 } if (5 & o[0]) throw o[1]; return { value: o[0] ? o[1] : void 0, done: !0 } }([o, u]) } } } function d(e, t) { for (var n in e) t.hasOwnProperty(n) || (t[n] = e[n]) } function h(e) { var t = "function" == typeof Symbol && Symbol.iterator, n = t && e[t], r = 0; if (n) return n.call(e); if (e && "number" == typeof e.length) return { next: function () { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e } } }; throw new TypeError(t ? "Object is not iterable." : "Symbol.iterator is not defined.") } function p(e, t) { var n = "function" == typeof Symbol && e[Symbol.iterator]; if (!n) return e; var r, i, o = n.call(e), s = []; try { for (; (void 0 === t || t-- > 0) && !(r = o.next()).done;)s.push(r.value) } catch (e) { i = { error: e } } finally { try { r && !r.done && (n = o.return) && n.call(o) } finally { if (i) throw i.error } } return s } function g() { for (var e = [], t = 0; t < arguments.length; t++)e = e.concat(p(arguments[t])); return e } function y() { for (var e = 0, t = 0, n = arguments.length; t < n; t++)e += arguments[t].length; var r = Array(e), i = 0; for (t = 0; t < n; t++)for (var o = arguments[t], s = 0, u = o.length; s < u; s++, i++)r[i] = o[s]; return r } function b(e) { return this instanceof b ? (this.v = e, this) : new b(e) } function w(e, t, n) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var r, i = n.apply(e, t || []), o = []; return r = {}, s("next"), s("throw"), s("return"), r[Symbol.asyncIterator] = function () { return this }, r; function s(e) { i[e] && (r[e] = function (t) { return new Promise((function (n, r) { o.push([e, t, n, r]) > 1 || u(e, t) })) }) } function u(e, t) { try { (n = i[e](t)).value instanceof b ? Promise.resolve(n.value.v).then(c, a) : l(o[0][2], n) } catch (e) { l(o[0][3], e) } var n } function c(e) { u("next", e) } function a(e) { u("throw", e) } function l(e, t) { e(t), o.shift(), o.length && u(o[0][0], o[0][1]) } } function v(e) { var t, n; return t = {}, r("next"), r("throw", (function (e) { throw e })), r("return"), t[Symbol.iterator] = function () { return this }, t; function r(r, i) { t[r] = e[r] ? function (t) { return (n = !n) ? { value: b(e[r](t)), done: "return" === r } : i ? i(t) : t } : i } } function _(e) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var t, n = e[Symbol.asyncIterator]; return n ? n.call(e) : (e = h(e), t = {}, r("next"), r("throw"), r("return"), t[Symbol.asyncIterator] = function () { return this }, t); function r(n) { t[n] = e[n] && function (t) { return new Promise((function (r, i) { (function (e, t, n, r) { Promise.resolve(r).then((function (t) { e({ value: t, done: n }) }), t) })(r, i, (t = e[n](t)).done, t.value) })) } } } function S(e, t) { return Object.defineProperty ? Object.defineProperty(e, "raw", { value: t }) : e.raw = t, e } function m(e) { if (e && e.__esModule) return e; var t = {}; if (null != e) for (var n in e) Object.hasOwnProperty.call(e, n) && (t[n] = e[n]); return t.default = e, t } function I(e) { return e && e.__esModule ? e : { default: e } } function P(e, t) { if (!t.has(e)) throw new TypeError("attempted to get private field on non-instance"); return t.get(e) } function D(e, t, n) { if (!t.has(e)) throw new TypeError("attempted to set private field on non-instance"); return t.set(e, n), n } }, function (e, t) { e.exports = require("electron") }, function (e, t) { e.exports = require("events") }, function (e, t) { e.exports = require("util") }, function (e, t, n) { "use strict"; Object.defineProperty(t, "__esModule", { value: !0 }); t.default = function (e) { return { title: "shortcut-capture", width: e.bounds.width, height: e.bounds.height, x: e.bounds.x, y: e.bounds.y, useContentSize: !0, frame: !1, show: !1, transparent: "darwin" === process.platform || "win32" === process.platform, resizable: !1, movable: !1, fullscreen: "darwin" !== process.platform, simpleFullscreen: !0, backgroundColor: "#30000000", titleBarStyle: "default", focusable: !0, enableLargerThanScreen: !0, skipTaskbar: "darwin" !== process.platform, minimizable: !1, maximizable: !1, hasShadow: !1, webPreferences: { nodeIntegration: !0 } } } }, function (e, t, n) { "use strict"; Object.defineProperty(t, "__esModule", { value: !0 }), t.events = { close: "ElectronShortcutCapture::CLOSE", download: "ElectronShortcutCapture::DOWNLOAD", clipboard: "ElectronShortcutCapture::CLIPBOARD", setCapturingDisplayId: "ElectronShortcutCapture::SETCAPTURINGDISPLAYID", receiveCapturingDisplayId: "ElectronShortcutCapture::RECEIVECAPTURINGDISPLAYID", screenSourcesToPng: "SCREENSOURCESTOPNG", loadedPageDisplayId: "LOADEDPAGEDISPLAYID" } }, function (e, t) { e.exports = require("fs") }, function (e, t) { e.exports = require("os") }, function (e, t) { e.exports = require("path") }]).default;