-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.xm
More file actions
132 lines (110 loc) · 4.38 KB
/
Tweak.xm
File metadata and controls
132 lines (110 loc) · 4.38 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
#define IS_IOS_8_OR_LATER ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)])
#define IS_MIN_IOS_VERSION(VER) ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:VER])
#define IOS_9_0 (NSOperatingSystemVersion){9, 0, 0}
#define IOS_10_0 (NSOperatingSystemVersion){10, 0, 0}
#define IOS_13_0 (NSOperatingSystemVersion){13, 0, 0}
#import <objc/runtime.h>
#include <mach-o/dyld.h>
NS_INLINE BOOL fakeUrlIfFileProtocol(NSMutableURLRequest *request) {
// get the original url
NSURL *originalUrl = [request URL];
// we won't make any changes if the URL doesn't start with file://
BOOL urlStartsWithFile = [[originalUrl absoluteString] hasPrefix:@"file://"];
if (urlStartsWithFile) {
// temporarily change URL so that Safari doesn't see we are actually trying to load a file:// URL
[request setURL: [NSURL URLWithString:@"http://www.google.com"]];
}
return urlStartsWithFile;
}
NS_INLINE BOOL shouldAllowNavigationActionWithRequest(NSMutableURLRequest *request) {
// if URL starts with file://, we should automatically allow the request
return [[[request URL] absoluteString] hasPrefix:@"file://"];
}
typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
WKNavigationActionPolicyCancel,
WKNavigationActionPolicyAllow,
};
typedef void (^DecisionHandlerBlock) (WKNavigationActionPolicy);
%group iOS10
%hook TabDocument
- (void)webView: (id) webview decidePolicyForNavigationAction: (id) action decisionHandler: (id) handler {
if (shouldAllowNavigationActionWithRequest([action performSelector: @selector(request)])) {
((DecisionHandlerBlock) handler)(WKNavigationActionPolicyAllow);
} else {
%orig;
}
}
%end
%end
%group iOS13
%hook TabDocument
- (void)_internalWebView: (id) webview decidePolicyForNavigationAction: (id) action preferences: (id) prefs decisionHandler: (id) handler {
if (shouldAllowNavigationActionWithRequest([action performSelector: @selector(request)])) {
((DecisionHandlerBlock) handler)(WKNavigationActionPolicyAllow);
} else {
%orig;
}
}
%end
%end
%group iOS9
%hook TabDocument
- (void)_decidePolicyForAction:(id)arg1 request:(id)arg2 inMainFrame:(_Bool)arg3 forNewWindow:(_Bool)arg4 currentURLIsFileURL:(_Bool)arg5 decisionHandler:(id)arg6 {
NSURL *originalUrl = [arg2 URL];
BOOL urlWasFaked = fakeUrlIfFileProtocol(arg2);
// call the original function
%orig;
if (urlWasFaked) {
// restore original file:// URL
[arg2 setURL:originalUrl];
}
}
%end
%end
%group iOS8
%hook TabDocument
- (void)_decidePolicyForAction:(id)arg1 request:(id)arg2 inMainFrame:(_Bool)arg3 forNewWindow:(_Bool)arg4 currentURLIsFileURL:(_Bool)arg5 decisionListener:(id)arg6 {
NSURL *originalUrl = [arg2 URL];
BOOL urlWasFaked = fakeUrlIfFileProtocol(arg2);
// call the original function
%orig;
if (urlWasFaked) {
// restore original file:// URL
[arg2 setURL:originalUrl];
}
}
%end
%end
%group BrowserControllerBugFix
NSString* badURL = @"file://";
NSString* goodURL = @"file:///";
%hook BrowserController
- (void) goToAddress: (NSString*) address {
if ([address isEqualToString: badURL]) {
%orig(goodURL);
} else {
%orig;
}
}
%end
%end
// select appropriate method to hook onto, based on the iOS version.
%ctor {
if IS_IOS_8_OR_LATER {
if IS_MIN_IOS_VERSION(IOS_13_0) {
%init(iOS13);
return;
} if IS_MIN_IOS_VERSION(IOS_10_0) {
%init(iOS10);
return;
} else if IS_MIN_IOS_VERSION(IOS_9_0) {
%init(iOS9);
} else {
%init(iOS8);
}
// just to make sure the method exists. Used to fix a nasty bug (seen in iOS 9) which crashes Safari when browsing to file://
if ([[%c(BrowserController) performSelector:@selector(sharedBrowserController)] respondsToSelector: @selector(goToAddress:)]) {
%init(BrowserControllerBugFix);
}
}
}