-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
1219 lines (1019 loc) · 37.4 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var savedRequest;
// 让你的代码高亮后支持换行 1.brPlugin 2.hljs.addPlugin(brPlugin);
const brPlugin = {
"before:highlightBlock": ({ block }) => {
block.innerHTML = block.innerHTML.replace(/\n/g, '').replace(/<br[ /]*>/g, '\n');
},
"after:highlightBlock": ({ result }) => {
result.value = result.value.replace(/\n/g, "<br>");
}
};
hljs.addPlugin(brPlugin);
/**
* 把控件认为是UIImageView
*/
let img = document.getElementById('show_image');
/**
* 把控件认为是UIButton
*/
let btn = document.getElementById('show_btn');
/**
* 把控件认为是UILabel
*/
let lab = document.getElementById('show_lab');
/**
* 自定义属性名的输入框
*/
let proNameInput = document.getElementById('proName');
/**
* 生成的代码包不包含属性引用getter方法
*/
let showPro = document.getElementById('show_pro');
/**
* 把控件认为是UIView里的线
*/
let showLine = document.getElementById('show_line');
/**
* 显示代码
*/
let msgDiv = document.getElementById('message')
/**
* 语言选择
*/
let language_select_element = document.getElementById('op')
/**
* 失去焦点时,替换属性名
*/
const inputHandler = function (e) {
let proName = e.target.value
if (proName.length > 1) {
let oldStr = msgDiv.innerText
if (lab.checked) {
// replaceAll https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
let new1 = oldStr.replaceAll('aLab', proName + 'Lab')
msgDiv.innerText = new1.replaceAll('statusLab', proName + 'Lab')
}
else if (btn.checked) {
let new1 = oldStr.replaceAll('aBtn', proName + 'Btn')
msgDiv.innerText = new1.replaceAll('useBtn', proName + 'Btn')
}
else if (img.checked) {
let new1 = oldStr.replaceAll('aImgV', proName + 'ImgV')
msgDiv.innerText = new1.replaceAll('bgImgV', proName + 'ImgV')
}
else if (showLine.checked) {
let new1 = oldStr.replaceAll('vLine', proName + 'View')
msgDiv.innerText = new1.replaceAll('bgImgV', proName + 'ImgV')
}
hljs.highlightAll()
}
}
// 监听来消息 getSource
chrome.runtime.onMessage.addListener(function (request, sender) {
let url = sender.tab.url;
if (request.action != "getSource") {
return
}
if (url.includes('cnblogs.com') || url.includes('localhost') || url.includes('pgyer.com')) {
// 在博客完时,只是追加日期而,面板不用显示出来
document.body.hidden = true
return
}
if (url.includes('lanhuapp.com/web') || url.includes('app.mockplus.cn')) {
savedRequest = request;
processCodeOutput(request)
return
}
// 其他情况直接显示返回的
message.innerText = request.source;
}
);
/**
* 刷新代码区域
* @param 参数request
*/
function processCodeOutput(request) {
let returnObj = request.source;
if(typeof returnObj === 'undefined') {
// 没有选中某个元素
// 显示下载xib文件按钮
document.getElementById("downloadXibFile").style.display="";
message.innerText = JSON.stringify(savedData);
msgDiv.className = 'language-json'
hljs.highlightAll()
return
}
// 隐藏下载xib文件按钮
document.getElementById("downloadXibFile").style.display="none";
// 多行 拼接 变量 ${变量名} innerText
let op = language_select_element.value;
if (op === 'swift') {
msgDiv.className = 'language-swift'
let hexColor = returnObj.hexColor
// 默认使用系统的
let textColorCode = `UIColor(red: ${returnObj.r}/255.0, green: ${returnObj.g}/255.0, blue: ${returnObj.b}/255.0, alpha: ${returnObj.a})`
if (typeof hexColor !== 'undefined') {
// 使用三方库SwiftHEXColors的方式
/*
输入 #4784F4 100%
输出1
lab.textColor = UIColor(hex: 0x4784F4)
输出2
lab.textColor = UIColor(hex: 0x4784f4, alpha:1)
*/
let hex = hexColor.split(' ')[0].replaceAll('#', '')
let alp = hexColor.split(' ')[1].replaceAll('%', '') / 100.0
if (hexColor.includes('100%')) {
textColorCode = `UIColor(hex: 0x${hex})`
} else {
textColorCode = `UIColor(hex: 0x${hex}, alpha:${alp})`
}
}
if (lab.checked) {
message.innerText = `\nlet aLab: UILabel = {
\tlet lab = UILabel()
\tlab.text = "${returnObj.text}"
\t// ${returnObj.hexColor}
\tlab.textColor = ${textColorCode}
\tlab.font = UIFont(name: "${returnObj.fontName}", size: ${returnObj.fontSize})
\tlab.translatesAutoresizingMaskIntoConstraints = false
\tview.addSubview(lab)
\tlab.snp.makeConstraints { (make) in
\t\tmake.centerX.equalToSuperview()
\t\tmake.top.equalToSuperview().offset(10)
\t}
\treturn lab
\t}()`
hljs.highlightAll()
return
}
if (btn.checked) {
message.innerText = `\nlet aBtn: UIButton = {
\tlet btn = UIButton(type: .custom)
\tbtn.setTitle("${returnObj.text}", for: .normal)
\tbtn.titleLabel?.font = UIFont(name: "${returnObj.fontName}", size: ${returnObj.fontSize})
\tbtn.translatesAutoresizingMaskIntoConstraints = false
\t// ${returnObj.hexColor}
\tbtn.setTitleColor(${textColorCode}), for: .normal)
\tview.addSubview(btn)
\tbtn.snp.makeConstraints { (make) in
\t\tmake.centerX.equalToSuperview()
\t\tmake.top.equalToSuperview().offset(130)
\t}
\treturn btn
\t}()`
hljs.highlightAll()
return
}
if (showLine.checked) {
/*
12,176,351,1,#F7F7F7,100
*/
message.innerText = `\nlet aLine: UIView = {
\tlet line = UIView()
\tline.isUserInteractionEnabled = false
\tline.translatesAutoresizingMaskIntoConstraints = false
\tline.backgroundColor = ${textColorCode}
\tview.addSubview(line)
\tline.snp.makeConstraints { (make) in
\t\tmake.top.equalToSuperview().offset(${returnObj.y})
\t\tmake.left.equalToSuperview().offset(${returnObj.x})
\t\tmake.size.equalTo(CGSize(width: ${returnObj.width}, height: ${returnObj.height}))
\t}
\treturn line
\t}()`
hljs.highlightAll()
return
}
if (img.checked) {
/*
24,141,320,20,识别到的字符串,fontWithName:@"PingFangSC-Medium" size,15,0x333333
*/
message.innerText = `\nlet aImgV: UIImageView = {
\tlet img = UIImage(named: "imgName")
\tlet imgV = UIImageView(image: img)
\timgV.backgroundColor = .red
\timgV.translatesAutoresizingMaskIntoConstraints = false
\tview.addSubview(imgV)
\timgV.snp.makeConstraints { (make) in
\t\tmake.top.equalToSuperview().offset(${returnObj.y})
\t\tmake.left.equalToSuperview().offset(${returnObj.x})
\t\tmake.size.equalTo(CGSize(width: ${returnObj.width}, height: ${returnObj.height}))
\t}
\treturn imgV
\t}()`
hljs.highlightAll()
return
}
return
}
if (op === 'dart') {
msgDiv.className = 'language-plaintext'
if (img.checked) {
message.innerText = `Image.asset('images/${returnObj.imgName}.png'),`;
hljs.highlightAll()
return
}
// 有可能是图片类型的按钮
let flutterColor = returnObj.hexColor
if (typeof flutterColor === 'undefined') {
message.innerText = `
IconButton(
icon: Image.asset('images/${returnObj.imgName}.png'),
onPressed: (){
},
),
`;
hljs.highlightAll()
// 没有颜色
return;
}
if(flutterColor.includes('#') &&
flutterColor.includes(' ') &&
flutterColor.includes('%')){
// #333333 100%
flutterColor = flutterColor.replaceAll('#', '')
if(flutterColor.split(' ')[1].includes('100')) {
flutterColor = '0xFF'+flutterColor.split(' ')[0];
}
}
if (lab.checked) {
message.innerText =`
Text(
\t'${returnObj.text}',
\tstyle: TextStyle(
\tfontFamily: '${returnObj.fontName}',
\tfontSize: ${returnObj.fontSize},
\tcolor: Color(${flutterColor})),
),`;
hljs.highlightAll()
return
}
if (showLine.checked) {
if(returnObj.corner === '0') {
message.innerText = `
Container(
// width:${returnObj.width},
// height:${returnObj.height},
// padding: EdgeInsets.fromLTRB(6, 5, 6, 5),
color: Color(${flutterColor}),
child: Text('未开始')
),
`;
hljs.highlightAll()
return
}
message.innerText =
`Container(
// width:${returnObj.width},
// height:${returnObj.height},
// padding: EdgeInsets.fromLTRB(6, 5, 6, 5),
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(${returnObj.corner}),
color: Color(${flutterColor}),
),
child: Text('未开始')
),`;
hljs.highlightAll()
return
}
if (btn.checked) {
message.innerText =
`TextButton(
\tonPressed: () {},
\tchild: Text(
\t\t'${returnObj.text}',
\t\tstyle: TextStyle(
\t\tfontFamily: '${returnObj.fontName}',
\t\tfontSize: ${returnObj.fontSize},
\t\tcolor: Color(${flutterColor})),
\t)),`;
hljs.highlightAll()
return
}
return
}
if (op === 'objc') {
msgDiv.className = 'language-objc'
// 类型判断 typeof strs === 'string'
// 以字符串开始 startsWith
if (img.checked) {
// 返回来的就是UIImageView
if (!showPro.checked) {
message.innerText = `UIImageView *aImgV = ({
\tNSString *name = @"图片名";
\tUIImage *img = [UIImage imageNamed:name];
\tUIImageView *imgV = [[UIImageView alloc] initWithImage:img];
\timgV.contentMode = UIViewContentModeScaleAspectFit;
\t#ifdef DEBUG
\timgV.backgroundColor = [UIColor redColor];
\t#endif
\t[self.view addSubview: imgV];
\t[imgV mas_makeConstraints:^(MASConstraintMaker *make) {
\t\tmake.top.equalTo(@${returnObj.y});
\t\tmake.leading.equalTo(@${returnObj.x});
\t\t//make.bottom.equalTo(@0);
\t\t//make.trailing.equalTo(@0);
\t\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t\t// make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t\t// make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t\t// make.width.equalTo(@${returnObj.width});
\t\t// make.height.equalTo(@${returnObj.height});
\t\t// make.size.mas_equalTo(CGSizeMake(${returnObj.width}, ${returnObj.height}));
\t\t// make.centerX.equalTo(@0);
\t\t// make.centerY.equalTo(@0);
\t}];
\timgV;
});
`} else {
message.innerText = `
@property(nonatomic) UIImageView *bgImgV;
-(UIImageView *)bgImgV {
if (!_bgImgV) {
\tNSString *name = @"图片名";
\tUIImage *img = [UIImage imageNamed:name];
\tUIImageView *imgV = [[UIImageView alloc] initWithImage:img];
\timgV.contentMode = UIViewContentModeScaleAspectFit;
\t#ifdef DEBUG
\timgV.backgroundColor = [UIColor redColor];
\t#endif
\t _bgImgV = imgV;
}
return _bgImgV;
}
[self.view addSubview: self.bgImgV];
\t[self.bgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
\t\tmake.top.equalTo(@${returnObj.y});
\t\tmake.leading.equalTo(@${returnObj.x});
\t\t//make.bottom.equalTo(@0);
\t\t//make.trailing.equalTo(@0);
\t\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t\t// make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t\t// make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t\t// make.width.equalTo(@${returnObj.width});
\t\t// make.height.equalTo(@${returnObj.height});
\t\t// make.size.mas_equalTo(CGSizeMake(${returnObj.width}, ${returnObj.height}));
\t\t// make.centerX.equalTo(@0);
\t\t// make.centerY.equalTo(@0);
}];
`}
hljs.highlightAll()
return
}
if (lab.checked) {
// 返回来的就是UILabel
if (!showPro.checked) {
if(typeof returnObj.returnCodeStr !== 'undefined') {
message.innerText = `${returnObj.returnCodeStr}
[self.view addSubview: label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
\tmake.top.equalTo(@${returnObj.y});
\tmake.leading.equalTo(@${returnObj.x});
\t//make.bottom.equalTo(@0);
\t//make.trailing.equalTo(@0);
\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t// make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t// make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t// make.centerX.equalTo(@0);
\t// make.centerY.equalTo(@0);
}];
`
hljs.highlightAll()
return
}
message.innerText = `UILabel *aLab = ({
\tUILabel *lab = [UILabel new];
\tlab.text = @"${returnObj.text}";
\tlab.font = [UIFont fontWithName:@"${returnObj.fontName}" size: ${returnObj.fontSize}];
\t// ${returnObj.hexColor}
\tlab.textColor =
\t[UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}];
\t[self.view addSubview: lab];
\t[lab mas_makeConstraints:^(MASConstraintMaker *make) {
\t\tmake.top.equalTo(@${returnObj.y});
\t\tmake.leading.equalTo(@${returnObj.x});
\t\t//make.bottom.equalTo(@0);
\t\t//make.trailing.equalTo(@0);
\t\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t\t// make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t\t// make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t\t// make.centerX.equalTo(@0);
\t\t// make.centerY.equalTo(@0);
\t}];
\tlab;
});
`
} else {
message.innerText = `
@property(nonatomic) UILabel *statusLab;
-(UILabel *)statusLab {
\tif (!_statusLab) {
\t\tUILabel *lab = [UILabel new];
\t\tlab.text = @"${returnObj.text}";
\t\tlab.font = [UIFont fontWithName:@"${returnObj.fontName}" size: ${returnObj.fontSize}];
\t\t// ${returnObj.hexColor}
\t\tlab.textColor =
\t\t[UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}];
\t\t_statusLab = lab;
\t}
\treturn _statusLab;
}
[self.view addSubview: self.statusLab];
[self.statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
\tmake.top.equalTo(@${returnObj.y});
\tmake.leading.equalTo(@${returnObj.x});
\t//make.bottom.equalTo(@0);
\t//make.trailing.equalTo(@0);
\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t// make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t// make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t// make.width.equalTo(@${returnObj.width});
\t// make.height.equalTo(@${returnObj.height});
\t// make.size.mas_equalTo(CGSizeMake(${returnObj.width}, ${returnObj.height}));
\t// make.centerX.equalTo(@0);
\t// make.centerY.equalTo(@0);
}];
`;
}
hljs.highlightAll()
return
}
if (btn.checked) {
if (returnObj.length == 2) {
// 纯图片按钮
if (!showPro.checked) {
message.innerText = `UIButton *aBtn = ({
\tUIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
\tNSString *name = @"图片名";
\tUIImage *img = [UIImage imageNamed:name];
\t[btn setImage:img forState:UIControlStateNormal];
\t[self.view addSubview: btn];
\t[btn mas_makeConstraints:^(MASConstraintMaker *make) {
\t\tmake.top.equalTo(@0);
\t\tmake.leading.equalTo(@0);
\t\tmake.bottom.equalTo(@0);
\t\tmake.trailing.equalTo(@0);
\t\t//make.top.equalTo(superView.mas_bottom).offset(0);
\t\t//make.leading.equalTo(superView.mas_leading).offset(0);
\t\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t\t// make.centerX.equalTo(@0);
\t\t// make.centerY.equalTo(@0);
\t }];
\t\tbtn;
\t});
`
hljs.highlightAll()
return
}
message.innerText =
`
\t@property(nonatomic) UIButton *useBtn;
\t
\t-(UIButton *)useBtn {
\t if (!_useBtn) {
\t UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
\t NSString *name = @"图片名";
\t UIImage *img = [UIImage imageNamed:name];
\t [btn setImage:img forState:UIControlStateNormal];
\t
\t _useBtn = btn;
\t }
\t return _useBtn;
}
[self.view addSubview: self.useBtn];
[self.useBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
\t make.leading.equalTo(@0);
\t //make.bottom.equalTo(@0);
\t //make.trailing.equalTo(@0);
\t //make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
// make.top.equalTo(superView.mas_bottom).offset(0);
// make.leading.equalTo(superView.mas_leading).offset(0);
// make.bottom.equalTo(superView.mas_bottom).offset(0);
// make.trailing.equalTo(superView.mas_trailing).offset(0);
// make.centerX.equalTo(@0);
// make.centerY.equalTo(@0);
}];
\t
`
hljs.highlightAll()
return
}
if (returnObj.length == 6 || returnObj.length == 7) {
// 纯背景色按钮 // 38,543,300,44,#9A2037,100,23
let corner = (returnObj.corner > 0) ? `btn.layer.cornerRadius = ${returnObj.corner};` : ''
if (!showPro.checked) {
message.innerText = `\nUIButton *aBtn = ({
\t UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
\t btn.backgroundColor = [UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}];
\t ${corner}
\t [self.view addSubview: btn];
\t [btn mas_makeConstraints:^(MASConstraintMaker *make) {
\t make.top.equalTo(@0);
\t make.leading.equalTo(@0);
\t //make.bottom.equalTo(@0);
\t //make.trailing.equalTo(@0);
\t //make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t // make.top.equalTo(superView.mas_bottom).offset(0);
\t // make.leading.equalTo(superView.mas_leading).offset(0);
\t // make.bottom.equalTo(superView.mas_bottom).offset(0);
\t // make.trailing.equalTo(superView.mas_trailing).offset(0);
\t // make.centerX.equalTo(@0);
\t // make.centerY.equalTo(@0);
\t }];
\t btn;
\t});
`
} else {
message.innerText =
`
\t@property(nonatomic) UIButton *useBtn;
\t
\t-(UIButton *)useBtn {
\t if (!_useBtn) {
\t UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
\t // ${returnObj.hexColor}
\t btn.backgroundColor =
\t [UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}];
\t ${corner}
\t
\t _useBtn = btn;
\t }
\t return _useBtn;
}
[self.view addSubview: self.useBtn];
[self.useBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
\t make.leading.equalTo(@0);
\t //make.bottom.equalTo(@0);
\t //make.trailing.equalTo(@0);
\t //make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
// make.top.equalTo(superView.mas_bottom).offset(0);
// make.leading.equalTo(superView.mas_leading).offset(0);
// make.bottom.equalTo(superView.mas_bottom).offset(0);
// make.trailing.equalTo(superView.mas_trailing).offset(0);
// make.centerX.equalTo(@0);
// make.centerY.equalTo(@0);
}];
\t
`
}
}
else {
// 纯文字按钮
// return [viewX, viewY, viewWidth, viewHeight, labStr, ocFontMethodName, labFontSizeStr, LabTextColorHexStr]
if (!showPro.checked) {
let setTitleCode = ''
if (typeof returnObj.text === 'undefined' || returnObj.text.length <= 0) {
// 没有文字
} else {
// 有文字
setTitleCode =
`\t[btn setTitle: @"${returnObj.text}" forState: UIControlStateNormal];
\tbtn.titleLabel.font = [UIFont fontWithName:@"${returnObj.fontName}" size: ${returnObj.fontSize}];
\t// ${returnObj.hexColor}
\t[btn setTitleColor: [UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}]
\t\tforState: UIControlStateNormal];
`
}
message.innerText = `\nUIButton *aBtn = ({
\tUIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
${setTitleCode}
\t// NSString *name = @"图片名";
\t// UIImage *img = [UIImage imageNamed:name];
\t// [btn setImage:img forState:UIControlStateNormal];
\t[self.view addSubview: btn];
\t[btn mas_makeConstraints:^(MASConstraintMaker *make) {
\t\tmake.top.equalTo(@0);
\t\tmake.leading.equalTo(@0);
\t\t//make.bottom.equalTo(@0);
\t\t//make.trailing.equalTo(@0);
\t\t//make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
\t\t// make.top.equalTo(superView.mas_bottom).offset(0);
\t\t// make.leading.equalTo(superView.mas_leading).offset(0);
\t\t// make.bottom.equalTo(superView.mas_bottom).offset(0);
\t\t// make.trailing.equalTo(superView.mas_trailing).offset(0);
\t\t// make.centerX.equalTo(@0);
\t\t// make.centerY.equalTo(@0);
\t}];
\tbtn;
});
`
} else {
message.innerText =
`
\t@property(nonatomic) UIButton *useBtn;
\t
-(UIButton *)useBtn {
\tif (!_useBtn) {
\t\tUIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
\t\t[btn setTitle: @"${returnObj.text}" forState: UIControlStateNormal];
\t\tbtn.titleLabel.font = [UIFont fontWithName:@"${returnObj.fontName}" size: ${returnObj.fontSize}];
\t\t// ${returnObj.hexColor}
\t\t[btn setTitleColor: [UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}]
\t\t\t forState: UIControlStateNormal];
\t\t// NSString *name = @"图片名";
\t\t// UIImage *img = [UIImage imageNamed:name];
\t\t// [btn setImage:img forState:UIControlStateNormal];
\t\t_useBtn = btn;
\t}
\treturn _useBtn;
}
\t
[self.view addSubview: self.useBtn];
[self.useBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
\t make.leading.equalTo(@0);
\t //make.bottom.equalTo(@0);
\t //make.trailing.equalTo(@0);
\t //make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
// make.top.equalTo(superView.mas_bottom).offset(0);
// make.leading.equalTo(superView.mas_leading).offset(0);
// make.bottom.equalTo(superView.mas_bottom).offset(0);
// make.trailing.equalTo(superView.mas_trailing).offset(0);
// make.centerX.equalTo(@0);
// make.centerY.equalTo(@0);
}];
\t
`
}
}
hljs.highlightAll()
return
}
if (showLine.checked) {
// 圆角
let cornerStgr = (returnObj.corner > 0) ? `line.layer.cornerRadius = ${returnObj.corner};` : ''
message.innerText = `\nUIView *vLine = ({
\t CGRect frame = CGRectMake(${returnObj.x}, ${returnObj.y}, ${returnObj.width}, ${returnObj.height});
\t UIView *line = [[UIView alloc] initWithFrame: frame];
\t line.userInteractionEnabled = NO;
\t// ${returnObj.hexColor}
\tline.backgroundColor =
\t[UIColor colorWithRed:${returnObj.r}/255.0 green:${returnObj.g}/255.0 blue:${returnObj.b}/255.0 alpha:${returnObj.a}];
\t ${cornerStgr}
\t [self.view addSubview: line];
\t [line mas_makeConstraints:^(MASConstraintMaker *make) {
\t\t make.top.equalTo(@${returnObj.y});
\t\t make.leading.equalTo(@${returnObj.x});
\t\t make.bottom.equalTo(@0);
\t\t make.trailing.equalTo(@0);
\t // make.top.equalTo(superView.mas_bottom).offset(${returnObj.y});
\t // make.leading.equalTo(superView.mas_leading).offset(${returnObj.x});
\t // make.bottom.equalTo(superView.mas_bottom).offset(0);
\t // make.trailing.equalTo(superView.mas_trailing).offset(0);
\t // make.width.equalTo(@${returnObj.width});
\t // make.height.equalTo(@${returnObj.height});
\t // make.size.mas_equalTo(CGSizeMake(${returnObj.width}, ${returnObj.height}));
\t // make.centerX.equalTo(@0);
\t // make.centerY.equalTo(@0);
\t }];
\t
\t line;
});`
hljs.highlightAll()
return
}
}
if (op === 'Android') {
if (lab.checked) {
/*
<TextView
android:layout_width="26dp"
android:layout_height="14dp"
android:text="吴京"
android:textColor="#ff333333"
android:textSize="13sp"
/>
{"x":"106.5dp",
"y":"266dp",
"width":"25.5dp",
"height":"13.5dp",
"corner":"0",
"r":"51",
"g":"51",
"b":"51",
"a":"1",
"text":"吴京",
"fontName":"MicrosoftYaHeiUI",
"fontSize":"13sp",
"hexColor":"#333333 100%"}
*/
msgDiv.className = 'language-xml'
message.innerText =
`<TextView
<!-- android:layout_width="${returnObj.width}" -->
<!-- android:layout_height="${returnObj.height}" -->
android:text="${returnObj.text}"
android:textColor="#ff333333"
android:textSize="${returnObj.fontSize}"
/>`
hljs.highlightAll()
}
}
}
function onWindowLoad() {
// 获取 popup.html里的元素进行字符串设定
var message = document.querySelector('#message');
// 注入脚本,接收错误回显
chrome.tabs.executeScript(null, {
file: "getPagesSource.js"
}, function () {
if (chrome.runtime.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;
}
});
proNameInput.addEventListener('blur', inputHandler);
}
// 窗口载入时使用自己的载入函数
window.onload = onWindowLoad;
/// 新加面板
document.addEventListener('DOMContentLoaded', function () {
// 默认配置
var defaultConfig = { 'op': 'objc', 'ocCode': 'btn', 'isShowPro': false };
// 读取数据,第一个参数是指定要读取的key以及设置默认值
chrome.storage.sync.get(defaultConfig, function (items) {
language_select_element.value = items.op;
let ocCodeStr = items.ocCode;
if (ocCodeStr === 'img') {
img.checked = true;
} else if (ocCodeStr === 'btn') {
btn.checked = true;
} else if (ocCodeStr === 'lab') {
lab.checked = true;
} else if (ocCodeStr === 'line') {
showLine.checked = true;
}
showPro.checked = (items.isShowPro === 'true');
// 同步自定义属性名输入框的显示与隐藏
proNameInput.hidden = !showPro.checked
});
});
/**
* 复制字符串到粘贴板
*/
function copyStr(str) {
// 复制字符串到粘贴板 http://www.voidcn.com/article/p-effxpdwn-buc.html
var input = document.createElement('textarea');
document.body.appendChild(input);
input.value = str;
input.focus();
input.select();
document.execCommand('Copy');
input.remove();
}
/**
* 保存配置事件
*/
function saveConfig() {
let op = language_select_element.value;
let ocCodeStr = '';
if (img.checked) {
ocCodeStr = 'img';
}
else if (btn.checked) {
ocCodeStr = 'btn';
}
else if (lab.checked) {
ocCodeStr = 'lab';
}
else if (showLine.checked) {
ocCodeStr = 'line';
}
let showProStr = showPro.checked ? "true" : "false"
let saveDict = {
op: op,
ocCode: ocCodeStr,
isShowPro: showProStr
};
// 保存配置到谷歌
chrome.storage.sync.set(saveDict, function () {
const status = document.getElementById('status')
status.style.display = 'block';
status.textContent = '保存成功!';
setTimeout(() => {
status.textContent = '';
status.style.display = 'none';
}, 800);
});
}
// 复制代码事件
document.getElementById('copyCode').addEventListener('click', function () {
copyStr(msgDiv.innerText)
});
// ------------------------------------下载xib文件-----------------------
document.getElementById('downloadXibFile').addEventListener('click', function () {
// 下载文件
let subViewStrs = ''
for (let aview of savedData['info']) {
// ["shape", "layer-group", "symbol", "bitmap", "text"]
if (aview['isVisible'] === true && aview['type'] === 'text') {
let textInfo = aview['font']
let textColorInfo = textInfo['color']
let r = textColorInfo.hasOwnProperty('r') ? textColorInfo['r'] : 0;
let g = textColorInfo.hasOwnProperty('g') ? textColorInfo['g'] : 0;
let b = textColorInfo.hasOwnProperty('b') ? textColorInfo['b'] : 0;
let a = textColorInfo.hasOwnProperty('a') ? textColorInfo['a'] : 1;
subViewStrs += getUILabelXMLString(aview['ddsOriginFrame']['x'], aview['ddsOriginFrame']['y'], aview['ddsOriginFrame']['width'], aview['ddsOriginFrame']['height'],
textInfo['content'],textInfo['font'],textInfo['size'],r, g,b,a);
}
else if (aview['isVisible'] === true && aview['type'] === 'shape') {
if (aview.hasOwnProperty('borders')) {
// UIView
let colorInfo = aview['borders'][0]['color']
let r = colorInfo.hasOwnProperty('r') ? colorInfo['r'] : 0;
let g = colorInfo.hasOwnProperty('g') ? colorInfo['g'] : 0;
let b = colorInfo.hasOwnProperty('b') ? colorInfo['b'] : 0;