From dfe39b45b56baf7195f30ae69d139ae0586e8770 Mon Sep 17 00:00:00 2001 From: BandarHelal <31299470+BandarHL@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:49:07 +0300 Subject: [PATCH] Clean up the code --- BHTwitter/BHTManager.h | 1 + BHTwitter/BHTManager.m | 3 +++ BHTwitter/BHTwitter.xm | 49 ++++++++++++++++++++---------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/BHTwitter/BHTManager.h b/BHTwitter/BHTManager.h index 767913cb..423ba2f7 100644 --- a/BHTwitter/BHTManager.h +++ b/BHTwitter/BHTManager.h @@ -51,5 +51,6 @@ + (BOOL)hideViewCount; + (BOOL)showTweetSource; + (BOOL)forceTweetFullFrame; ++ (BOOL)stripTrackingParams; @end diff --git a/BHTwitter/BHTManager.m b/BHTwitter/BHTManager.m index b61915d4..7b68a292 100644 --- a/BHTwitter/BHTManager.m +++ b/BHTwitter/BHTManager.m @@ -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]; diff --git a/BHTwitter/BHTwitter.xm b/BHTwitter/BHTwitter.xm index 5c28fd98..cc1c007e 100644 --- a/BHTwitter/BHTwitter.xm +++ b/BHTwitter/BHTwitter.xm @@ -1442,34 +1442,31 @@ static NSDictionary*> *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* 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 *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* 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 *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; }