-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tweak.x
339 lines (242 loc) · 11.2 KB
/
Tweak.x
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
// Hooks are from https://github.com/opa334/Choicy/blob/master/ChoicySB/TweakSB.x
// This code implements new features to https://github.com/lint/PasteAndGo/, so please make sure to go star their repo as well! ;)
#import "PasteAndGo2.h"
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
%group iOS13Up
%hook SBIconView
%new
-(bool) isBrowser:(NSString *)bundleID { // hardcoded function because I'm tired and lazy
if ([bundleID isEqualToString:@"com.apple.mobilesafari"]
|| [bundleID isEqualToString:@"org.mozilla.ios.Firefox"]
|| [bundleID isEqualToString:@"org.mozilla.ios.Focus"]
|| [bundleID isEqualToString:@"com.google.chrome.ios"]
|| [bundleID isEqualToString:@"com.brave.ios.browser"]
|| [bundleID isEqualToString:@"com.microsoft.msedge"]) {
return true;
}
return false;
}
-(NSArray *) applicationShortcutItems {
NSArray * orig = %orig;
//MARK: App bundle and bundle IDs
NSString * bundleID;
NSBundle * tweakBundle = [NSBundle bundleWithPath:@"/Library/Application Support/PasteAndGo2.bundle"];
if ([self respondsToSelector:@selector(applicationBundleIdentifier)]){
bundleID = [self applicationBundleIdentifier];
} else if ([self respondsToSelector:@selector(applicationBundleIdentifierForShortcuts)]){
bundleID = [self applicationBundleIdentifierForShortcuts];
}
if(!bundleID){
return orig;
}
if ([self isBrowser:bundleID]) {
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *pbStr = [pasteBoard string];
if (pbStr){
NSURL *url = [NSURL URLWithString:[pbStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
if ([[UIApplication sharedApplication] canOpenURL:url]) { // Item copied is an URL
SBSApplicationShortcutItem* pasteAndGoItem = [[%c(SBSApplicationShortcutItem) alloc] init];
pasteAndGoItem.localizedTitle = [tweakBundle localizedStringForKey:@"PASTEANDGO" value:@"" table:nil];
pasteAndGoItem.localizedSubtitle = [NSString stringWithFormat: @"Go to: %@", [[[pbStr stringByReplacingOccurrencesOfString:@"https://" withString:@""] stringByReplacingOccurrencesOfString:@"http://" withString:@""] mutableCopy]]; // link without http:// and https://
pasteAndGoItem.type = @"com.twickd.amodrono.pasteandgo2.item";
return [orig arrayByAddingObject:pasteAndGoItem];
} else { // Item copied is not an URL
SBSApplicationShortcutItem* pasteAndGoItem = [[%c(SBSApplicationShortcutItem) alloc] init];
pasteAndGoItem.localizedTitle = [tweakBundle localizedStringForKey:@"PASTEANDSEARCH" value:@"" table:nil];
pasteAndGoItem.localizedSubtitle = [NSString stringWithFormat: @"Search \"%@\"", pbStr];
pasteAndGoItem.type = @"com.twickd.amodrono.pasteandgo2.item";
return [orig arrayByAddingObject:pasteAndGoItem];
}
}
}
return orig;
}
+(void) activateShortcut:(SBSApplicationShortcutItem*)item withBundleIdentifier:(NSString*)bundleID forIconView:(id)iconView {
if ([[item type] isEqualToString:@"com.twickd.amodrono.pasteandgo2.item"]){
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *pbStr = [pasteBoard string];
if (pbStr) {
pbStr = [pbStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * urlScheme;
BOOL needToEscapeURL = NO;
BOOL needToRemoveSchema = NO;
if ([bundleID isEqualToString:@"org.mozilla.ios.Firefox"]) {
urlScheme = @"firefox://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"org.mozilla.ios.Focus"]) {
urlScheme = @"firefox-focus://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"com.google.chrome.ios"]) {
if ([pbStr hasPrefix:@"https://"])
urlScheme = @"googlechromes://";
else
urlScheme = @"googlechrome://";
needToRemoveSchema = YES;
} else if ([bundleID isEqualToString:@"com.brave.ios.browser"]) {
urlScheme = @"brave://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"com.microsoft.msedge"]) {
urlScheme = @"microsoft-edge-";
} else {
urlScheme = @"";
}
NSCharacterSet *customCharacterset = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]\\<>^`{|} "] invertedSet];
if (needToRemoveSchema) {
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"http://" withString:@""];
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"https://" withString:@""];
}
NSURL * finalURL = [NSURL URLWithString:pbStr];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:pbStr]]) {
if (needToEscapeURL) pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
finalURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", urlScheme, pbStr]];
} else { // item is not an url, so we need to search it
if (needToEscapeURL) {
pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"%" withString:@"%25"]; // second level escape is needed for the query
pbStr = [NSString stringWithFormat:@"%@https://www.google.com/search%%3Fq%%3D%@", urlScheme, pbStr];
} else {
pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
pbStr = [NSString stringWithFormat:@"%@%@www.google.com/search?q=%@", urlScheme, needToRemoveSchema?@"":@"https://", pbStr];
}
finalURL = [NSURL URLWithString: pbStr];
}
HBLogDebug(@"Final URL to open: %@", finalURL);
[[UIApplication sharedApplication] openURL:finalURL];
}
}
%orig;
}
%end
%end
%group iOS12OrDown
%hook SBUIAppIconForceTouchControllerDataProvider
%new
-(bool) isBrowser:(NSString *)bundleID {
if ([bundleID isEqualToString:@"com.apple.mobilesafari"]
|| [bundleID isEqualToString:@"org.mozilla.ios.Firefox"]
|| [bundleID isEqualToString:@"org.mozilla.ios.Focus"]
|| [bundleID isEqualToString:@"com.google.chrome.ios"]
|| [bundleID isEqualToString:@"com.brave.ios.browser"]
|| [bundleID isEqualToString:@"com.microsoft.msedge"]) {
return true;
}
return false;
}
-(NSArray *) applicationShortcutItems {
NSArray *orig = %orig;
//MARK: App bundle and bundle IDs
NSString * bundleID;
NSBundle * tweakBundle = [NSBundle bundleWithPath:@"/Library/Application Support/PasteAndGo2.bundle"];
if (!bundleID){
return orig;
}
if ([self isBrowser:bundleID]) {
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *pbStr = [pasteBoard string];
if (pbStr){
NSURL *url = [NSURL URLWithString:[pbStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
if ([[UIApplication sharedApplication] canOpenURL:url]) { // Item copied is an URL
SBSApplicationShortcutItem* pasteAndGoItem = [[%c(SBSApplicationShortcutItem) alloc] init];
pasteAndGoItem.localizedTitle = [tweakBundle localizedStringForKey:@"PASTEANDGO" value:@"" table:nil];
pasteAndGoItem.localizedSubtitle = [NSString stringWithFormat: @"Go to: %@", [[[pbStr stringByReplacingOccurrencesOfString:@"https://" withString:@""] stringByReplacingOccurrencesOfString:@"http://" withString:@""] mutableCopy]]; // link without http:// and https://
pasteAndGoItem.type = @"com.twickd.amodrono.pasteandgo2.item";
if (!orig){
return @[pasteAndGoItem];
} else {
return [orig arrayByAddingObject:pasteAndGoItem];
}
} else { // Item copied is not an URL
SBSApplicationShortcutItem* pasteAndGoItem = [[%c(SBSApplicationShortcutItem) alloc] init];
pasteAndGoItem.localizedTitle = [tweakBundle localizedStringForKey:@"PASTEANDSEARCH" value:@"" table:nil];
pasteAndGoItem.localizedSubtitle = [NSString stringWithFormat: @"Search \"%@\"", pbStr];
pasteAndGoItem.type = @"com.twickd.amodrono.pasteandgo2.item";
if (!orig) {
return @[pasteAndGoItem];
} else {
return [orig arrayByAddingObject:pasteAndGoItem];
}
}
}
}
return orig;
}
/*
%new
-(bool) isInArray:(NSArray)array item:(NSString)item {
for (NSString * currentString in array) {
if ([currentString isEqualToString:item]) {
return true;
}
return false;
}
*/
%end
%hook SBUIAppIconForceTouchController
-(void) appIconForceTouchShortcutViewController:(id)arg1 activateApplicationShortcutItem:(SBSApplicationShortcutItem*)item {
if ([[item type] isEqualToString:@"com.twickd.amodrono.pasteandgo2.item"]){
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *pbStr = [pasteBoard string];
NSString * bundleID;
if (!bundleID) {
return %orig;
}
if (pbStr) {
pbStr = [pbStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * urlScheme;
BOOL needToEscapeURL = NO;
BOOL needToRemoveSchema = NO;
if ([bundleID isEqualToString:@"org.mozilla.ios.Firefox"]) {
urlScheme = @"firefox://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"org.mozilla.ios.Focus"]) {
urlScheme = @"firefox-focus://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"com.google.chrome.ios"]) {
if ([pbStr hasPrefix:@"https://"])
urlScheme = @"googlechromes://";
else
urlScheme = @"googlechrome://";
needToRemoveSchema = YES;
} else if ([bundleID isEqualToString:@"com.brave.ios.browser"]) {
urlScheme = @"brave://open-url?url=";
needToEscapeURL = YES;
} else if ([bundleID isEqualToString:@"com.microsoft.msedge"]) {
urlScheme = @"microsoft-edge-";
} else {
urlScheme = @"";
}
NSCharacterSet *customCharacterset = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]\\<>^`{|} "] invertedSet];
if (needToRemoveSchema) {
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"http://" withString:@""];
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"https://" withString:@""];
}
NSURL * finalURL = [NSURL URLWithString:pbStr];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:pbStr]]) {
if (needToEscapeURL) pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
finalURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", urlScheme, pbStr]];
} else { // item is not an url, so we need to search it
if (needToEscapeURL) {
pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
pbStr = [pbStr stringByReplacingOccurrencesOfString:@"%" withString:@"%25"]; // second level escape is needed for the query
pbStr = [NSString stringWithFormat:@"%@https://www.google.com/search%%3Fq%%3D%@", urlScheme, pbStr];
} else {
pbStr = [pbStr stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
pbStr = [NSString stringWithFormat:@"%@%@www.google.com/search?q=%@", urlScheme, needToRemoveSchema?@"":@"https://", pbStr];
}
finalURL = [NSURL URLWithString: pbStr];
}
HBLogDebug(@"Final URL to open: %@", finalURL);
[[UIApplication sharedApplication] openURL:finalURL];
}
}
%orig;
}
%end
%end
%ctor {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")){
%init(iOS13Up);
} else {
%init(iOS12OrDown);
}
}