Skip to content

Commit

Permalink
Clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
BandarHL committed Jan 13, 2023
1 parent b5f284d commit dfe39b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions BHTwitter/BHTManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
+ (BOOL)hideViewCount;
+ (BOOL)showTweetSource;
+ (BOOL)forceTweetFullFrame;
+ (BOOL)stripTrackingParams;
@end

3 changes: 3 additions & 0 deletions BHTwitter/BHTManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ + (BOOL)showTweetSource {
+ (BOOL)forceTweetFullFrame {
return [[NSUserDefaults standardUserDefaults] boolForKey:@"force_tweet_full_frame"];
}
+ (BOOL)stripTrackingParams {
return [[NSUserDefaults standardUserDefaults] boolForKey:@"strip_tracking_params"];
}

+ (UIViewController *)BHTSettingsWithAccount:(TFNTwitterAccount *)twAccount {
SettingsViewController *pref = [[SettingsViewController alloc] initWithTwitterAccount:twAccount];
Expand Down
49 changes: 23 additions & 26 deletions BHTwitter/BHTwitter.xm
Original file line number Diff line number Diff line change
Expand Up @@ -1442,34 +1442,31 @@ static NSDictionary<NSString*, NSArray<NSString*>*> *trackingParams = @{@"twitte
static NSString *_lastCopiedURL;

%ctor {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
// Someone needs to hold reference the to Notification
_PasteboardChangeObserver = [center addObserverForName:UIPasteboardChangedNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification * _Nonnull note){
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"strip_tracking_params"]){
if(UIPasteboard.generalPasteboard.hasURLs){
NSURL *pasteboardURL = UIPasteboard.generalPasteboard.URL;
NSArray<NSString*>* params = trackingParams[pasteboardURL.host];

if([pasteboardURL.absoluteString isEqualToString:_lastCopiedURL] == NO && params != nil && pasteboardURL.query != nil){
// to prevent endless copy loop
_lastCopiedURL = pasteboardURL.absoluteString;
NSURLComponents *cleanedURL = [NSURLComponents componentsWithURL:pasteboardURL resolvingAgainstBaseURL:NO];
NSMutableArray<NSURLQueryItem*> *safeParams = [NSMutableArray arrayWithCapacity:0];

for (NSURLQueryItem *item in cleanedURL.queryItems){
if([params containsObject:item.name] == NO){
[safeParams addObject:item];
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
// Someone needs to hold reference the to Notification
_PasteboardChangeObserver = [center addObserverForName:UIPasteboardChangedNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note){
if ([BHTManager stripTrackingParams]) {
if (UIPasteboard.generalPasteboard.hasURLs) {
NSURL *pasteboardURL = UIPasteboard.generalPasteboard.URL;
NSArray<NSString*>* params = trackingParams[pasteboardURL.host];

if ([pasteboardURL.absoluteString isEqualToString:_lastCopiedURL] == NO && params != nil && pasteboardURL.query != nil) {
// to prevent endless copy loop
_lastCopiedURL = pasteboardURL.absoluteString;
NSURLComponents *cleanedURL = [NSURLComponents componentsWithURL:pasteboardURL resolvingAgainstBaseURL:NO];
NSMutableArray<NSURLQueryItem*> *safeParams = [NSMutableArray arrayWithCapacity:0];

for (NSURLQueryItem *item in cleanedURL.queryItems) {
if ([params containsObject:item.name] == NO) {
[safeParams addObject:item];
}
cleanedURL.queryItems = safeParams;
UIPasteboard.generalPasteboard.URL = cleanedURL.URL;
}
cleanedURL.queryItems = safeParams;
UIPasteboard.generalPasteboard.URL = cleanedURL.URL;
}
}
}];
%init;
}
}];
%init;
}

0 comments on commit dfe39b4

Please sign in to comment.