UNPKG

electron-shortcut-capture-patch

Version:

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

16 lines (14 loc) 18.1 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=1)}([function(e,t){e.exports=require("os")},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(2),i=n(3),o=n(0),s=n(4),u=n(5),a=n(6),c=function(){function e(t){var n=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){n.getLogger("setScreenInfo-display"+s.inspect(e)),n.screenInfo[e.id]={},n.screenInfo.cutWidth=n.screenInfo.cutWidth&&n.screenInfo.cutWidth>e.size.width*e.scaleFactor?n.screenInfo.cutWidth:e.size.width*e.scaleFactor,n.screenInfo.cutHeight=n.screenInfo.cutHeight&&n.screenInfo.cutHeight>e.size.height*e.scaleFactor?n.screenInfo.cutHeight:e.size.height*e.scaleFactor},this.initWin=function(){return r.__awaiter(n,void 0,void 0,(function(){var t,n=this;return r.__generator(this,(function(r){return t="darwin"===o.platform(),this.displays=i.screen.getAllDisplays(),this.getLogger("初始化截图"),this.captureWins=this.displays.map((function(e){n.setScreenInfo(e);var r=new i.BrowserWindow(u.default(e));return r.displayId=e.id,t||(r.setBounds({x:e.bounds.x,y:e.bounds.y}),r.setSkipTaskbar(!0)),r})),this.getLogger("设备数量:"+this.captureWins.length),this.captureWins.forEach((function(t){t.loadURL(e.URL)})),[2]}))}))},this.show=function(){return r.__awaiter(n,void 0,void 0,(function(){var e,t,n,i,o,u,c,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,c=o.mousePoint,l=c.x-u.bounds.x,f=c.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(a.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]}}))}))},this.getScreenSources=function(e,t){return r.__awaiter(n,void 0,void 0,(function(){return r.__generator(this,(function(n){switch(n.label){case 0:return[4,i.desktopCapturer.getSources({types:["screen"],thumbnailSize:{width:e,height:t}})];case 1:return[2,n.sent()]}}))}))},this.listenEsc=function(){i.globalShortcut.register("esc",(function(){n.hide()}))},this.unListenEsc=function(){i.globalShortcut.unregister("esc")},this.bindKey=function(){n.key&&(console.log(n.key),i.globalShortcut.register(n.key,(function(){return r.__awaiter(n,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{n.key&&i.globalShortcut.unregister(n.key),n.key=e,i.globalShortcut.register(e,(function(){return r.__awaiter(n,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){n.key=""}else try{i.globalShortcut.unregister(n.key),n.key=e}catch(e){n.key=""}},this.listenDisplayNumChange=function(){i.screen.on("display-metrics-changed",(function(){n.reInit()})),i.screen.on("display-added",(function(){n.reInit()})),i.screen.on("display-removed",(function(){n.reInit()}))},this.reInit=function(){n.getLogger("重新初始化"),n.screenInfo={},n.loadedPageDisplayIds=[],n.captureWins.forEach((function(e){e.close()})),n.initWin()},this.getLoadedPageDisplayId=function(){i.ipcMain.on(a.events.loadedPageDisplayId,(function(e,t){console.log("loadedPageDisplayId",t),n.loadedPageDisplayIds.includes(t)||(n.loadedPageDisplayIds.push(t),n.getLogger("加载完的显示器id:"+t),n.loadedPageDisplayIds.length===n.captureWins.length&&n.captureWins.forEach((function(e){e.hide()})))}))},this.shortCutScreenIsOpened=function(){return n.multiScreen?n.captureWins.every((function(e){return e.isVisible()})):n.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=!!t&&!!t.multiScreen,this.downloadFileprefix=t&&t.downloadFileprefix||"",this.onClipboard=t?t.onClipboard:null,this.key=t?t.key:"",this.onHide=t?t.onHide:null,this.onShow=t?t.onShow:null,this.onShowByKey=t?t.onShowByKey:null,this.getLogger=t&&t.getLogger?function(e){t.getLogger(e)}:function(e){console.log(e)},this.initWin(),this.bindHide(),this.bindClipboard(),this.bindDownload(),this.listenCapturingDisplayId(),this.bindKey(),this.listenDisplayNumChange(),this.getLoadedPageDisplayId()}return e.prototype.bindHide=function(){var e=this;i.ipcMain.on(a.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(a.events.receiveCapturingDisplayId,0),n.setVisibleOnAllWorkspaces(!1),e&&n.webContents.send(a.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(a.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"),a=e.downloadFileprefix+(new Date).getTime()+".png",c=i.dialog.showSaveDialogSync({defaultPath:a});if(!c)return e.isDownloading=!1,e.getLogger("取消下载");try{n(7).writeFileSync(c,u)}catch(t){e.getLogger("下载失败:"+t)}e.isDownloading=!1}))},e.prototype.bindClipboard=function(){var e=this;i.ipcMain.on(a.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(a.events.setCapturingDisplayId,(function(t,n){e.captureWins.forEach((function(e){e.webContents.send(a.events.receiveCapturingDisplayId,n)}))}))},e.URL="file://"+n(8).join(__dirname,"../renderer/index.html"),e}();t.default=c},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 a})),n.d(t,"__metadata",(function(){return c})),n.d(t,"__awaiter",(function(){return l})),n.d(t,"__generator",(function(){return f})),n.d(t,"__createBinding",(function(){return d})),n.d(t,"__exportStar",(function(){return h})),n.d(t,"__values",(function(){return p})),n.d(t,"__read",(function(){return g})),n.d(t,"__spread",(function(){return y})),n.d(t,"__spreadArrays",(function(){return b})),n.d(t,"__await",(function(){return w})),n.d(t,"__asyncGenerator",(function(){return v})),n.d(t,"__asyncDelegator",(function(){return _})),n.d(t,"__asyncValues",(function(){return S})),n.d(t,"__makeTemplateObject",(function(){return m})),n.d(t,"__importStar",(function(){return I})),n.d(t,"__importDefault",(function(){return P})),n.d(t,"__classPrivateFieldGet",(function(){return D})),n.d(t,"__classPrivateFieldSet",(function(){return O})); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ 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 a(e,t){return function(n,r){t(n,r,e)}}function c(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{a(r.next(e))}catch(e){o(e)}}function u(e){try{a(r.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,u)}a((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=s.trys,(i=i.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,n,r){void 0===r&&(r=n),e[r]=t[n]}function h(e,t){for(var n in e)"default"===n||t.hasOwnProperty(n)||(t[n]=e[n])}function p(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 g(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 y(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(g(arguments[t]));return e}function b(){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 w(e){return this instanceof w?(this.v=e,this):new w(e)}function v(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 w?Promise.resolve(n.value.v).then(a,c):l(o[0][2],n)}catch(e){l(o[0][3],e)}var n}function a(e){u("next",e)}function c(e){u("throw",e)}function l(e,t){e(t),o.shift(),o.length&&u(o[0][0],o[0][1])}}function _(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:w(e[r](t)),done:"return"===r}:i?i(t):t}:i}}function S(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,n=e[Symbol.asyncIterator];return n?n.call(e):(e=p(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 m(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e}function I(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 P(e){return e&&e.__esModule?e:{default:e}}function D(e,t){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return t.get(e)}function O(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("util")},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0);t.default=function(e){var t="darwin"===r.platform(),n={title:"shortcut-capture",width:e.bounds.width,height:e.bounds.height,useContentSize:!0,frame:!1,show:!1,transparent:!1,resizable:!1,movable:!1,fullscreen:!t,simpleFullscreen:!0,titleBarStyle:"default",focusable:!0,enableLargerThanScreen:!0,skipTaskbar:!t,minimizable:!1,maximizable:!1,hasShadow:!1,webPreferences:{nodeIntegration:!0,contextIsolation:!1,enableRemoteModule:!0,webSecurity:!1}};return t&&(n.x=e.bounds.x,n.y=e.bounds.y),n}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.events=void 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("path")}]).default;