Skip to content

Commit

Permalink
feat(iOS): 新增JS脚本插件 (didi#1023)
Browse files Browse the repository at this point in the history
* 新增JS脚本插件

* 优化页面切换效果

* update README

Co-authored-by: carefree <[email protected]>
  • Loading branch information
moliya and carefree authored May 31, 2022
1 parent edea679 commit ef0c242
Show file tree
Hide file tree
Showing 26 changed files with 509 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ DoKit 是一个功能平台,能够让每一个 App 快速接入一些常用的
10. **【NSLog】** 把所有 NSLog 信息打印到UI界面,避免没有开发证书无法调试的尴尬;
11. **【Lumberjack】** 每一条 CocoaLumberjack 的日志信息,都在在 App 的界面中显示出来,再也不需要导出日志这么麻烦;(iOS独有)
12. **【DBView】** 通过网页方便快捷的操作应用内数据库,让数据库的调试变得非常优雅;
13. **【模拟弱网】** 限制网速,模拟弱网环境下App的运行情况。(android独有)
13. **【模拟弱网】** 限制网速,模拟弱网环境下App的运行情况;(android独有)
14. **【JS脚本】** 在指定WebView运行JS脚本。(iOS独有)

### 三、性能检测

Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ DoKit is rich in functions, easy to access, and easy to expand. Everyone is welc
* Log:print all logs to the UI interface for easy viewing
* UserDefaults(iOS): add, delete, and modify the NSUserDefaults file
* DBView:perform more detailed operations on the DB file on the web
* JavaScript(iOS):execute scripts in the web view


### Performance Tools
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions iOS/DoraemonKit/Resource/en.lproj/Doraemon.strings
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@

//UserDefaults

//JS脚本
"JS脚本" = "JavaScript";
"请选择WebView" = "Select WebView";
"无可用的WebView" = "No Available WebView";
"脚本列表" = "Script List";
"脚本执行" = "Execute Script";
"JS代码" = "JS Code";
"脚本不能为空" = "Code can not be empty";

//CocoaLumberjack
"Lumberjack" = "Lumberjack";
"CocoaLumberjack日志记录" = "CocoaLumberjack";
Expand Down
9 changes: 9 additions & 0 deletions iOS/DoraemonKit/Resource/zh-Hans.lproj/Doraemon.strings
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@

//UserDefaults

//JS脚本
"JS脚本" = "JavaScript";
"请选择WebView" = "请选择WebView";
"无可用的WebView" = "无可用的WebView";
"脚本列表" = "脚本列表";
"脚本执行" = "脚本执行";
"JS代码" = "JS代码";
"脚本不能为空" = "脚本不能为空";

//CocoaLumberjack
"Lumberjack" = "Lumberjack";
"CocoaLumberjack日志记录" = "CocoaLumberjack日志记录";
Expand Down
6 changes: 6 additions & 0 deletions iOS/DoraemonKit/Src/Core/Cache/DoraemonCacheManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
- (void)clearAllH5historicalRecord;
- (void)clearH5historicalRecordWithText:(NSString *)text;

/// JS历史脚本
- (NSArray<NSDictionary *> *)jsHistoricalRecord;
- (NSString *)jsHistoricalRecordForKey:(NSString *)key;
- (void)saveJsHistoricalRecordWithText:(NSString *)text forKey:(NSString *)key;
- (void)clearJsHistoricalRecordWithKey:(NSString *)key;

/// 保存启动类
- (void)saveStartClass : (NSString *)startClass;
- (NSString *)startClass;
Expand Down
63 changes: 63 additions & 0 deletions iOS/DoraemonKit/Src/Core/Cache/DoraemonCacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
static NSString * const kDoraemonMethodUseTimeKey = @"doraemon_method_use_time_key";
static NSString * const kDoraemonLargeImageDetectionKey = @"doraemon_large_image_detection_key";
static NSString * const kDoraemonH5historicalRecord = @"doraemon_historical_record";
static NSString * const kDoraemonJsHistoricalRecord = @"doraemon_js_historical_record";
static NSString * const kDoraemonStartTimeKey = @"doraemon_start_time_key";
static NSString * const kDoraemonStartClassKey = @"doraemon_start_class_key";
static NSString * const kDoraemonANRTrackKey = @"doraemon_anr_track_key";
Expand Down Expand Up @@ -265,6 +266,68 @@ - (void)clearH5historicalRecordWithText:(NSString *)text {
[_defaults synchronize];
}

- (NSArray<NSDictionary *> *)jsHistoricalRecord {
return [_defaults arrayForKey:kDoraemonJsHistoricalRecord];
}

- (NSString *)jsHistoricalRecordForKey:(NSString *)key {
NSArray *history = [self jsHistoricalRecord] ?: @[];
for (NSDictionary *dict in history) {
//是否同名配置
if ([[dict objectForKey:@"key"] isEqualToString:key]) {
return [dict objectForKey:@"value"];
}
}
return nil;
}

- (void)saveJsHistoricalRecordWithText:(NSString *)text forKey:(NSString *)key {
NSString *saveKey = [NSString stringWithFormat:@"%.0f", NSDate.date.timeIntervalSince1970];
if (key.length > 0) {
saveKey = key;
}
NSMutableArray *list = [NSMutableArray array];
BOOL matched = NO;
NSArray *history = [self jsHistoricalRecord] ?: @[];
for (NSDictionary *dict in history) {
//是否同名配置
if ([[dict objectForKey:@"key"] isEqualToString:saveKey]) {
[list addObject:@{
@"key": saveKey,
@"value": text
}];
matched = YES;
continue;
}
[list addObject:dict];
}
if (!matched) {
[list insertObject:@{
@"key": saveKey,
@"value": text
} atIndex:0];
}
[_defaults setObject:list forKey:kDoraemonJsHistoricalRecord];
[_defaults synchronize];
}

- (void)clearJsHistoricalRecordWithKey:(NSString *)key {
if (!key) {
return;
}
NSMutableArray *list = [NSMutableArray array];
NSArray *history = [self jsHistoricalRecord] ?: @[];
for (NSDictionary *dict in history) {
//是否同名配置
if ([[dict objectForKey:@"key"] isEqualToString:key]) {
continue;
}
[list addObject:dict];
}
[_defaults setObject:list forKey:kDoraemonJsHistoricalRecord];
[_defaults synchronize];
}

- (void)saveStartClass : (NSString *)startClass {
[_defaults setObject:startClass forKey:kDoraemonStartClassKey];
[_defaults synchronize];
Expand Down
2 changes: 2 additions & 0 deletions iOS/DoraemonKit/Src/Core/Category/UIView+Doraemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@

- (UIViewController *)doraemon_viewController;

- (NSArray *)doraemon_findViewsForClass:(Class)clazz;

@end

11 changes: 11 additions & 0 deletions iOS/DoraemonKit/Src/Core/Category/UIView+Doraemon.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,15 @@ -(UIViewController *)doraemon_viewController{
return nil;
}

- (NSArray *)doraemon_findViewsForClass:(Class)clazz {
NSMutableArray *result = [NSMutableArray array];
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:clazz]) {
[result addObject:subview];
}
[result addObjectsFromArray:[subview doraemon_findViewsForClass:clazz]];
}
return result;
}

@end
2 changes: 2 additions & 0 deletions iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ typedef NS_ENUM(NSUInteger, DoraemonManagerPluginType) {
DoraemonManagerPluginType_DoraemonDatabasePlugin,
// NSUserDefaults工具
DoraemonManagerPluginType_DoraemonNSUserDefaultsPlugin,
// JS脚本
DoraemonManagerPluginType_DoraemonJavaScriptPlugin,

#pragma mark - 性能检测
// 帧率监控
Expand Down
9 changes: 9 additions & 0 deletions iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ - (void)initData{
#if DoraemonWithDatabase
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonDatabasePlugin];
#endif
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonJavaScriptPlugin];

#pragma mark - 性能检测
[self addPluginWithPluginType:DoraemonManagerPluginType_DoraemonFPSPlugin];
Expand Down Expand Up @@ -570,6 +571,14 @@ - (DoraemonManagerPluginTypeModel *)getDefaultPluginDataWithPluginType:(Doraemon
@{kAtModule:DoraemonLocalizedString(@"常用工具")},
@{kBuriedPoint:@"dokit_sdk_comm_ck_userdefault"}
],
@(DoraemonManagerPluginType_DoraemonJavaScriptPlugin) : @[
@{kTitle:DoraemonLocalizedString(@"JS脚本")},
@{kDesc:DoraemonLocalizedString(@"JS脚本")},
@{kIcon:@"doraemon_js"},
@{kPluginName:@"DoraemonJavaScriptPlugin"},
@{kAtModule:DoraemonLocalizedString(@"常用工具")},
@{kBuriedPoint:@"dokit_sdk_comm_ck_js"}
],

// 性能检测
@(DoraemonManagerPluginType_DoraemonFPSPlugin) : @[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// DoraemonJavaScriptDetailViewController.h
// DoraemonKit
//
// Created by carefree on 2022/5/11.
//

#import "DoraemonBaseViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface DoraemonJavaScriptDetailViewController : DoraemonBaseViewController

@property (nonatomic, copy) NSString *key;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// DoraemonJavaScriptDetailViewController.m
// DoraemonKit
//
// Created by carefree on 2022/5/11.
//

#import "DoraemonJavaScriptDetailViewController.h"
#import "DoraemonKit.h"
#import "DoraemonDefine.h"
#import "DoraemonToastUtil.h"
#import "DoraemonCacheManager.h"
#import "DoraemonJavaScriptManager.h"

@interface DoraemonJavaScriptDetailViewController ()

@property (nonatomic, weak) UITextView *textView;

@end

@implementation DoraemonJavaScriptDetailViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.title = DoraemonLocalizedString(@"脚本执行");
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(runScript)];
UIEdgeInsets edge = UIEdgeInsetsMake(10, 10, 0, 10);
CGFloat width = self.view.bounds.size.width - edge.left - edge.right;
CGFloat height = self.view.bounds.size.height - edge.top - edge.bottom;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(edge.left, edge.top + IPHONE_NAVIGATIONBAR_HEIGHT, width, 30)];
titleLabel.text = DoraemonLocalizedString(@"JS代码");

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(edge.left, CGRectGetMaxY(titleLabel.frame) + edge.top, width, height - 200)];
textView.layer.borderWidth = 1 / UIScreen.mainScreen.scale;
textView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
textView.layer.cornerRadius = 6;
textView.font = [UIFont systemFontOfSize:16];
textView.textContainerInset = UIEdgeInsetsMake(8, 3, 8, 3);

[self.view addSubview:titleLabel];
[self.view addSubview:textView];
self.textView = textView;

if (self.key.length > 0) {
self.textView.text = [DoraemonCacheManager.sharedInstance jsHistoricalRecordForKey:self.key];
}
}

#pragma mark - Private
- (void)runScript {
NSString *value = self.textView.text;
if (value.length == 0) {
[DoraemonToastUtil showToastBlack:@"脚本不能为空" inView:self.view];
return;
}
[DoraemonCacheManager.sharedInstance saveJsHistoricalRecordWithText:value forKey:self.key];
[DoraemonManager.shareInstance hiddenHomeWindow];
[DoraemonJavaScriptManager.shareInstance evalJavaScript:value];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// DoraemonJavaScriptPlugin.h
// AFNetworking
//
// Created by carefree on 2022/5/11.
//

#import <Foundation/Foundation.h>
#import "DoraemonPluginProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@interface DoraemonJavaScriptPlugin : NSObject<DoraemonPluginProtocol>

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// DoraemonJavaScriptPlugin.m
// AFNetworking
//
// Created by carefree on 2022/5/11.
//

#import "DoraemonJavaScriptPlugin.h"
#import "DoraemonJavaScriptManager.h"
#import "DoraemonHomeWindow.h"

@implementation DoraemonJavaScriptPlugin

- (void)pluginDidLoad {
[[DoraemonHomeWindow shareInstance] hide];
[[DoraemonJavaScriptManager shareInstance] show];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// DoraemonJavaScriptViewController.h
// DoraemonKit
//
// Created by carefree on 2022/5/11.
//

#import "DoraemonBaseViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface DoraemonJavaScriptViewController : DoraemonBaseViewController

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit ef0c242

Please sign in to comment.