forked from Kapeli/Dash-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDHJavaScriptBridge.m
More file actions
141 lines (124 loc) · 5.13 KB
/
DHJavaScriptBridge.m
File metadata and controls
141 lines (124 loc) · 5.13 KB
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
//
// Copyright (C) 2016 Kapeli
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "DHJavaScriptBridge.h"
#import "DHWebViewController.h"
#import "DHCSS.h"
@implementation DHJavaScriptBridge
+ (DHJavaScriptBridge *)sharedBridge
{
static dispatch_once_t pred;
static DHJavaScriptBridge *_singleton = nil;
dispatch_once(&pred, ^{
_singleton = [[DHJavaScriptBridge alloc] init];
_singleton.alertBlock = ^(JSValue *message) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JavaScript Alert" message:[message toString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
};
});
return _singleton;
}
- (void)switchAppleLanguage:(JSValue *)nameValue
{
NSString *name = [nameValue toString];
name = [name trimWhitespace];
if(name && name.length)
{
[[NSUserDefaults standardUserDefaults] setObject:name forKey:DHActiveAppleLanguageKey];
[[DHCSS sharedCSS] refreshActiveCSS];
[[DHWebViewController sharedWebViewController] reload];
if([DHRemoteServer sharedServer].connectedRemote)
{
[[DHRemoteServer sharedServer] sendObject:@{@"language": name} forRequestName:@"syncAppleLanguage" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
}
}
- (void)newSwitchAppleLanguage:(JSValue *)nameValue
{
NSString *text = [[nameValue toString] trimWhitespace];
if([text contains:@"Swift"])
{
[DHAppleActiveLanguage setLanguage:DHNewActiveAppleLanguageSwift];
}
else if([text contains:@"Objective-C"] || [text contains:@"ObjC"])
{
[DHAppleActiveLanguage setLanguage:DHNewActiveAppleLanguageObjC];
}
if([DHRemoteServer sharedServer].connectedRemote)
{
[[DHRemoteServer sharedServer] sendObject:@{@"language": @([DHAppleActiveLanguage currentLanguage])} forRequestName:@"syncNewAppleLanguage" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
}
- (void)unityConsoleLog:(NSString *)message
{
if([message isKindOfClass:[JSValue class]])
{
message = [(JSValue*)message toString];
}
if([message isKindOfClass:[NSString class]] && [message isEqualToString:@"selected"])
{
NSString *value = [[[DHWebViewController sharedWebViewController] webView] stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('cSelect-Selected')[0].innerText"];
if(value && value.length)
{
[[NSUserDefaults standardUserDefaults] setObject:value forKey:@"unitySelectedSnippetLanguage"];
}
}
}
- (void)coffeeScriptOpenLink_:(NSString *)string
{
NSURL *url = [NSURL URLWithString:[@"http://coffeescript.org/" stringByAppendingString:string]];
if(url)
{
[[UIApplication sharedApplication] openURL:url];
}
}
- (void)showFallbackExplanation
{
[[DHRemoteServer sharedServer] sendObject:@{@"selector": @"showFallbackExplanation", @"shouldShowWindow": @YES} forRequestName:@"performWebSelector" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
- (void)loadFallbackURL_:(JSValue *)suppressButtonChecked
{
[[DHRemoteServer sharedServer] sendObject:@{@"selector": @"loadFallbackURL:", @"arg": @([suppressButtonChecked toBool])} forRequestName:@"performWebSelector" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
- (void)openDownloads
{
[[DHRemoteServer sharedServer] sendObject:@{@"selector": @"openDownloads"} forRequestName:@"performWebSelector" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
- (void)openDocsets
{
[[DHRemoteServer sharedServer] sendObject:@{@"selector": @"openDocsets"} forRequestName:@"performWebSelector" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
- (void)openProfiles
{
[[DHRemoteServer sharedServer] sendObject:@{@"selector": @"openProfiles", @"shouldShowWindow": @YES} forRequestName:@"performWebSelector" encrypted:YES toMacName:[DHRemoteServer sharedServer].connectedRemote.name];
}
- (void)openGuide
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://kapeli.com/dash_guide"]];
}
- (void)openIOSLink
{
[UIAlertView showWithTitle:@"Dash for iOS" message:@"You're using it!" cancelButtonTitle:@"Okay" otherButtonTitles:nil tapBlock:nil];
}
- (void)webViewDidChangeLocationWithinPage
{
[[DHWebViewController sharedWebViewController] webViewDidChangeLocationWithinPage];
}
- (void)log:(JSValue *)value
{
NSLog(@"JS Log: %@", value);
}
@end