-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tweak.xm
224 lines (171 loc) · 9.2 KB
/
Tweak.xm
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
//
// Tweak.xm
// Zetime-Goodies
//
// Created by cbs_ghost on 2017/10/15.
// Copyright (c) 2017 CbS Ghost. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import "UIImage-ResizeMagick/UIImage+ResizeMagick.h"
@interface APPSShowTips : UIView
+ (void)hidden;
+ (void)showLoading;
+ (void)showLoadingWithMessage:(nullable NSString *)msg;
@end
@interface UIImageView (WebCache)
- (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable id)completedBlock;
@end
@interface AppsViewController : UIViewController
@end
@interface APPSCustomPhotoViewControl : AppsViewController <WKNavigationDelegate>
- (void)wallpaperTap;
- (void)setEditImage:(nullable UIImage *)image;
- (void)setSendImage:(nullable UIImage *)image;
// Custom methods
- (void)webViewFailureCallback:(WKWebView *)wkWebView;
@end
%hook APPSCustomPhotoViewControl
- (void)wallpaperTap
{
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:[@"Select wallpaper source" uppercaseString]
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
// Cancel button tappped.
//[self dismissViewControllerAnimated:YES completion:^{}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Built-in"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// Built-in button tapped.
%orig;
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"zetime.daap.dk" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// zetime.daap.dk button tapped.
[%c(APPSShowTips) showLoading];
// Tweak webview's width on mobile device
AppsViewController *communityWebViewController = [[[%c(AppsViewController) alloc] init] autorelease];
NSString *webViewFixWidthJS = @"var meta = document.createElement('meta'); "
@"meta.name = 'viewport'; meta.content = 'width=device-width, user-scalable=no'; "
@"document.getElementsByTagName('head')[0].appendChild(meta);";
if (self.view.bounds.size.width < 480) {
webViewFixWidthJS = [webViewFixWidthJS stringByReplacingOccurrencesOfString:@"width=device-width"
withString:@"width=480"];
}
WKUserScript *webViewFixWidthUserScript = [[[WKUserScript alloc] initWithSource:webViewFixWidthJS
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES] autorelease];
WKUserContentController *communityWebViewUserContentController = [[[WKUserContentController alloc] init] autorelease];
[communityWebViewUserContentController addUserScript:webViewFixWidthUserScript];
// WKWebView configuration
WKWebViewConfiguration *communityWebViewConfiguration = [[[WKWebViewConfiguration alloc] init] autorelease];
communityWebViewConfiguration.userContentController = communityWebViewUserContentController;
// Alloc webview
WKWebView *communityWebView = [[[WKWebView alloc] initWithFrame:self.view.bounds
configuration:communityWebViewConfiguration] autorelease];
communityWebView.navigationDelegate = self;
[communityWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://zetime.daap.dk/"]]];
[communityWebViewController.view addSubview: communityWebView];
communityWebViewController.title = @"zetime.daap.dk";
// Go!!
[self.navigationController pushViewController:communityWebViewController animated:YES];
}]];
// Present action sheet
[self.navigationController presentViewController:actionSheet animated:YES completion:nil];
}
%new
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
NSURL *url = navigationResponse.response.URL;
NSString *mimeType = navigationResponse.response.MIMEType;
// Simple url redirecting prevention
if (![navigationResponse.response.URL.host isEqualToString:@"zetime.daap.dk"]) {
UIAlertController *redirectAlert = [UIAlertController alertControllerWithTitle:@"Redirection Error"
message:@"You've been redirected to an unexpected website.\n"
@"This error message is to prevent your ZeTime app from malicious code injection."
preferredStyle:UIAlertControllerStyleAlert];
[redirectAlert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// Go back
[self.navigationController popViewControllerAnimated:YES];
}]];
// Present alert view
[%c(APPSShowTips) hidden];
[self.navigationController presentViewController:redirectAlert animated:YES completion:nil];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if ([mimeType isEqualToString:@"image/png"] || [mimeType isEqualToString:@"image/jpeg"]) {
[%c(APPSShowTips) showLoadingWithMessage:@"Downloading..."];
// Set watchface!!
UIImageView *myImageView = MSHookIvar<UIImageView *>(self, "_myImageView");
[myImageView sd_setImageWithURL:url completed:^{
[UIImage setInterpolationQuality:kCGInterpolationHigh];
UIImage *resizeImage = [[myImageView image]
resizedImageByMagick:@"240x240#"];
[self setEditImage:resizeImage];
[self setSendImage:resizeImage];
[self.navigationController popViewControllerAnimated:YES];
[%c(APPSShowTips) hidden];
}];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
%new
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
// Set website's title
self.navigationController.navigationBar.topItem.title = webView.title;
[%c(APPSShowTips) hidden];
}
%new
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
// Filter out unnecessary error
if (error.code < 0 && error.code != NSURLErrorCancelled && error.code != NSURLErrorUserCancelledAuthentication) {
[self webViewFailureCallback:webView];
}
}
%new
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
// Filter out unnecessary error
if (error.code < 0 && error.code != NSURLErrorCancelled && error.code != NSURLErrorUserCancelledAuthentication) {
[self webViewFailureCallback:webView];
}
}
%new
- (void)webViewFailureCallback:(WKWebView *)wkWebView
{
UIAlertController *failAlert = [UIAlertController alertControllerWithTitle:@"Connection Error"
message:@"Unable to connect to zetime.daap.dk. :("
preferredStyle:UIAlertControllerStyleAlert];
// Cancel and go back
[failAlert addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
}]];
// Retry (reload webpage)
[failAlert addAction:[UIAlertAction actionWithTitle:@"Retry"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
if (wkWebView.URL == nil) {
[wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://zetime.daap.dk/"]]];
} else {
[wkWebView reload];
}
}]];
// Present alert view
[%c(APPSShowTips) hidden];
[self.navigationController presentViewController:failAlert animated:YES completion:nil];
}
%end