1+ /* patch wechat devtools begin */
2+ ( ( ) => {
3+ try {
4+ const originalSpawn = require ( "child_process" ) . spawn ;
5+ require ( "child_process" ) . spawn = function ( command , args , options ) {
6+ if ( command . includes ( "wcc.exe" ) ) {
7+ command = command . replace ( "code/package.nw" , "package.nw" ) ;
8+ command = command . replace ( "wcc.exe" , "wcc" ) ;
9+ } else if ( command . includes ( "wcsc.exe" ) ) {
10+ command = command . replace ( "code/package.nw" , "package.nw" ) ;
11+ command = command . replace ( "wcsc.exe" , "wcsc" ) ;
12+ }
13+ return originalSpawn . apply ( this , [ command , args , options ] ) ;
14+ } ;
15+ const originalResolve = require ( "path" ) . resolve ;
16+ require ( "path" ) . resolve = function ( ...paths ) {
17+ if ( paths . length === 2 && paths [ 1 ] . includes ( "code/package.nw" ) ) {
18+ paths [ 1 ] = paths [ 1 ] . replace ( "code/package.nw" , "package.nw" ) ;
19+ }
20+ return originalResolve . apply ( this , paths ) ;
21+ } ;
22+ if ( typeof nw === "undefined" ) {
23+ return ;
24+ }
25+
26+ let log = function ( content ) {
27+ process . stderr . write ( content + "\n" ) ;
28+ } ;
29+
30+ let originMenuItem = nw . MenuItem ;
31+ nw . MenuItem = function MenuItem ( options ) {
32+
33+ options = Object . assign ( { } , options ) ;
34+
35+ delete options . shortcutName ;
36+ delete options . shouldEnabled ;
37+
38+ if ( options . label && ( typeof options . label === "string" ) ) {
39+
40+ if ( options . label . indexOf ( "[" ) !== - 1 ) {
41+ let rest = options . label . split ( "[" ) . slice ( 1 ) . join ( "[" ) . trim ( ) ;
42+ if ( rest [ rest . length - 1 ] === "]" ) {
43+ rest = rest . slice ( 0 , - 1 ) . split ( "+" ) . map ( ( x ) => {
44+ if ( ! x ) { return "+" }
45+ switch ( x ) {
46+ case "↓" : { return "Down" ; }
47+ case "↑" : { return "Up" ; }
48+ case "PAGE↓" : { return "PageDown" ; }
49+ case "PAGE↑" : { return "PageUp" ; }
50+ case "←" : { return "Left" ; }
51+ case "→" : { return "Right" ; }
52+ default : { return x ; }
53+ }
54+ } ) ;
55+ if ( rest . length > 1 ) {
56+ options . key = rest [ rest . length - 1 ] ;
57+ options . modifiers = rest . slice ( 0 , - 1 ) . join ( "+" ) ;
58+ } else {
59+ options . key = rest [ 0 ] ;
60+ }
61+ }
62+ options . label = options . label . split ( "[" ) [ 0 ] ;
63+ }
64+
65+ if ( options . label . indexOf ( "(&" ) !== - 1 ) {
66+ options . label = options . label . split ( "(&" ) [ 0 ] ;
67+ }
68+ options . label = options . label . replace ( "&" , "" ) . trim ( ) ;
69+
70+ switch ( options . label ) {
71+ case "Go to Declaration" : { options . label = "转到声明" ; break ; }
72+ case "Go to References" : { options . label = "转到引用" ; break ; }
73+ case "Find All References" : { options . label = "查找所有引用" ; break ; }
74+ case "Find All Implementations" : { options . label = "查找所有实现" ; break ; }
75+ }
76+
77+ }
78+
79+ return new originMenuItem ( options ) ;
80+
81+ } ;
82+
83+ let originAppend = nw . Menu . prototype . append ;
84+ nw . Menu . prototype . append = function ( item ) {
85+
86+ if ( item . parentMenu ) {
87+ item . parentMenu . remove ( item ) ;
88+ }
89+ item . parentMenu = this ;
90+
91+ if ( ( this . items . length > 0 ) &&
92+ ( this . items [ this . items . length - 1 ] . type === "separator" ) &&
93+ ( item . type === "separator" ) ) {
94+ originInsert . call ( this , item , this . items . length ) ;
95+ return ;
96+ }
97+
98+ if ( ( this . items . length === 0 ) && ( item . type === "separator" ) ) {
99+ originInsert . call ( this , item , this . items . length ) ;
100+ return ;
101+ }
102+
103+ return originAppend . call ( this , item ) ;
104+ } ;
105+
106+ let originInsert = nw . Menu . prototype . insert ;
107+ nw . Menu . prototype . insert = function ( item , index ) {
108+ if ( item . parentMenu ) {
109+ item . parentMenu . remove ( item ) ;
110+ }
111+ item . parentMenu = this ;
112+ return originInsert . call ( this , item , index ) ;
113+ } ;
114+ const originalOpen = nw . Window . open
115+ nw . Window . open = function ( url , options , callback ) {
116+ console . warn ( '[wechat-devtools] nw.Window.open is called, url:' , url , 'options:' , options ) ;
117+ let cb = callback
118+ if ( options . title === '版本更新提示' ) {
119+ options . inject_js_start = 'js/unpack/hackrequire/index.js' ;
120+ cb = ( ...args ) => {
121+ const keys = [
122+ "shareData" ,
123+ "windowMap" ,
124+ "isSimple" ,
125+ "masterProxyPort" ,
126+ "proxyPort" ,
127+ "masterH2ProxyPort" ,
128+ "h2ProxyPort"
129+ ] ;
130+ for ( let k of keys )
131+ args [ 0 ] . window . global [ k ] = global [ k ] ;
132+ callback ( ...args )
133+ }
134+ }
135+ else if ( options . title === '云开发控制台' ) {
136+ cb = ( ...args ) => {
137+ const keys = [
138+ "shareData" ,
139+ "windowMap" ,
140+ "isSimple" ,
141+ "masterProxyPort" ,
142+ "proxyPort" ,
143+ "masterH2ProxyPort" ,
144+ "h2ProxyPort" ,
145+ 'tokenData' ,
146+ ] ;
147+ for ( let k of keys )
148+ args [ 0 ] . window . global [ k ] = global [ k ] ;
149+ callback ( ...args )
150+ }
151+ }
152+ else if ( options . title . includes ( '微信开发者工具' ) ) {
153+ cb = ( ...args ) => {
154+ const keys = [
155+ "shareData" ,
156+ "windowMap" ,
157+ "isSimple" ,
158+ "masterProxyPort" ,
159+ "proxyPort" ,
160+ "masterH2ProxyPort" ,
161+ "h2ProxyPort" ,
162+ 'tokenData' ,
163+ ] ;
164+ for ( let k of keys )
165+ args [ 0 ] . window . global [ k ] = global [ k ] ;
166+ callback ( ...args )
167+ }
168+ }
169+ return originalOpen . apply ( this , [ url , options , cb ] )
170+ }
171+ const originalExec = require ( 'child_process' ) . exec ;
172+ require ( 'child_process' ) . exec = function ( command , options , callback ) {
173+ if ( command . includes ( 'open -a Terminal' ) ) {
174+ command = 'gnome-terminal'
175+ }
176+ return originalExec . apply ( this , [ command , options , callback ] )
177+ }
178+ } catch ( error ) {
179+ process . stderr . write ( error . message ) ;
180+ process . stderr . write ( error . stack ) ;
181+ }
182+ } ) ( ) ;
183+ /* patch wechat devtools end */
0 commit comments