Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: links you copy or share lose Spotify's tracking

My-Name-Is-Jeff300c26098679
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 040a77c7..bfa7a905 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -76,7 +76,8 @@ Shared:
Hub JSON (AdHubs.x) and the feeds (Feeds.m), Premium pop-ups dropped (AdPopups.x), and the responses
rewritten on the way in (AdNetwork.x, Premium.m over the protobuf walker in Protobuf.m), with crossfade
and automix switched on in the player core and crossfade's switch kept in step with its slider (Crossfade.x)
- Privacy/ telemetry blocking and its counters
+ Privacy/ telemetry blocking and its counters, and the tracking taken off shared open.spotify.com links
+ (CleanLinks.x over the cleaner in CleanLinks.m)
ArtistBlock/ tracks by blocked artists skipped as they start (ArtistSkip.x), the list and the Blocked artists page under Player
Flags/ Spotify's remote-config flags: the provider hook, the generated table, the All flags page and the Labs page
Gestures/ the double tap zones on the player: the grid, what each cell does, the recognizer (each look hooks it on)
@@ -407,7 +408,7 @@ four heights) and the Home flags, the parts of Home to hide including the DJ but
playlist header, buttons and pills to hide, and the Library flags. Then Premium, ads & privacy
(EeveeSpotify's Hide ads and Hide upsells, in the native look hiding the video carousel in Search, and
an Ad and upsell flags page under them, every switch there forcing a flag Spotify ships on to off;
-Spoof Premium; Block telemetry; then what the ad
+Spoof Premium; Block telemetry; Clean shared links; then what the ad
blocking and the telemetry blocking have stopped) and Labs (features Spotify built and did not ship,
AI Chat (Martini) first). Last, All flags, Spotify's remote-config flags with a search field and an
Auto / Off / On control per flag (a text field for the number and text ones), and Mod: Updates
diff --git a/harness/clean-links/README.md b/harness/clean-links/README.md
new file mode 100644
index 00000000..f6376ada
--- /dev/null
+++ b/harness/clean-links/README.md
@@ -0,0 +1,18 @@
+# Clean shared links checks
+
+Two checks for `Shared/Privacy/`'s Clean shared links.
+
+ ./build.sh && build/clean-links-check
+
+runs `CleanLinks.m` on the Mac: si, nd, pt, context and utm_* taken off open.spotify.com links, t and every
+other parameter kept in order, the intl-xx/ path and the fragment kept, other hosts, spotify.link and spotify:
+URIs left alone, and links found in a sentence without the full stop or bracket after them.
+
+ THEOS=$HOME/theos SIM=<udid> ./build-sim.sh
+
+builds `CleanLinks.x` with logos.pl's internal generator and runs `sim/main.m` in that simulator with
+`simctl spawn`: each pasteboard setter on a pasteboard of its own (never the general one), a share sheet's
+string and URL items, a provider's `-item`, a subclass's, an item source's answer, and the switch turned off.
+
+Both print ok or FAIL per line and exit with the number of failures. What neither covers: which of these
+Spotify's own Copy link and share sheet take on a phone.
diff --git a/harness/clean-links/build-sim.sh b/harness/clean-links/build-sim.sh
new file mode 100755
index 00000000..681f8ebc
--- /dev/null
+++ b/harness/clean-links/build-sim.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+# Builds CleanLinks.x's hooks with the check in sim/ for the iOS simulator and runs it in the booted one, or in the
+# simulator named by SIM (a UDID): logos.pl's internal generator, so no substrate is needed.
+set -e
+cd "$(dirname "$0")"
+SRC=../../tweak/Sources
+mkdir -p build/gen
+"$THEOS/bin/logos.pl" -c generator=internal "$SRC/Shared/Privacy/CleanLinks.x" > build/gen/CleanLinks.m
+SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)
+xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -O0 -g -isysroot "$SDK" \
+ -I "$SRC" -I "$SRC/Shared/Privacy" \
+ sim/main.m build/gen/CleanLinks.m "$SRC"/Shared/Privacy/CleanLinks.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGLog.m \
+ -framework UIKit -framework Foundation -o build/clean-links-sim
+xcrun simctl spawn "${SIM:-booted}" "$PWD/build/clean-links-sim"
diff --git a/harness/clean-links/build.sh b/harness/clean-links/build.sh
new file mode 100755
index 00000000..025478bd
--- /dev/null
+++ b/harness/clean-links/build.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+# Builds the clean shared links check for the Mac: Shared/Privacy/CleanLinks.m as the tweak compiles it, shim/
+# standing in for Privacy.h's UIKit.
+set -e
+cd "$(dirname "$0")"
+SRC=../../tweak/Sources
+mkdir -p build
+xcrun clang -fobjc-arc -O0 -g -Wall -Werror -target arm64-apple-macos13.0 -I shim -I "$SRC" -I "$SRC/Shared/Privacy" \
+ check.m "$SRC"/Shared/Privacy/CleanLinks.m -framework Foundation -o build/clean-links-check
+echo "built build/clean-links-check"
diff --git a/harness/clean-links/check.m b/harness/clean-links/check.m
new file mode 100644
index 00000000..2831f436
--- /dev/null
+++ b/harness/clean-links/check.m
@@ -0,0 +1,69 @@
+// Checks Clean shared links (Shared/Privacy/CleanLinks.m) on the Mac: the links Spotify copies, the tracking
+// taken off them and everything else left as it came.
+//
+// ./build.sh && build/clean-links-check
+//
+// Every line prints ok or FAIL; the exit status is the number of failures.
+#import <Foundation/Foundation.h>
+#import "Privacy.h"
+
+static int failures;
+
+static void check(NSString *got, NSString *wanted, NSString *what) {
+ BOOL ok = [got isEqualToString:wanted];
+ printf("%s %s\n", ok ? "ok " : "FAIL", what.UTF8String);
+ if (!ok) printf(" got %s\n wanted %s\n", got.UTF8String, wanted.UTF8String);
+ failures += !ok;
+}
+
+static NSString *url(NSString *link) {
+ return SGCleanLinkURL([NSURL URLWithString:link]).absoluteString;
+}
+
+int main(void) {
+ @autoreleasepool {
+ NSString *track = @"https://open.spotify.com/track/4uLU6hMCjMI75M1A2tKUQC";
+ check(url([track stringByAppendingString:@"?si=a1b2c3"]), track, @"si alone goes, and the ? with it");
+ check(url([track stringByAppendingString:@"?si=a1b2c3&utm_source=copy-link&utm_medium=x"]), track,
+ @"si and utm_* go");
+ check(url([track stringByAppendingString:@"?SI=a1&UTM_Campaign=x&nd=1&pt=abc&context=spotify%3Aplaylist%3Ax"]), track,
+ @"names compared without case; nd, pt and context go");
+ check(url(@"https://open.spotify.com/episode/abc?si=x&t=30"), @"https://open.spotify.com/episode/abc?t=30",
+ @"t stays");
+ check(url(@"https://open.spotify.com/episode/abc?t=30&go=1&si=x"), @"https://open.spotify.com/episode/abc?t=30&go=1",
+ @"what stays keeps its order");
+ check(url(@"https://open.spotify.com/intl-de/album/abc?si=x#frag"), @"https://open.spotify.com/intl-de/album/abc#frag",
+ @"intl-xx/ path and the fragment stay");
+ check(url(@"https://OPEN.Spotify.com/track/abc?si=x"), @"https://OPEN.Spotify.com/track/abc",
+ @"host compared without case");
+ check(url(@"https://example.com/track/abc?si=x"), @"https://example.com/track/abc?si=x", @"another host is left alone");
+ check(url(@"https://spotify.link/abc?si=x"), @"https://spotify.link/abc?si=x", @"a spotify.link short link is left alone");
+ check(url(@"https://open.spotify.com/track/abc?q=a%26b&si=x"), @"https://open.spotify.com/track/abc?q=a%26b",
+ @"a kept value stays encoded as it came");
+ check(url(@"mailto:[email protected]?si=x"), @"mailto:[email protected]?si=x", @"a link with no host is left alone");
+ check(url(@"/track/abc?si=x"), @"/track/abc?si=x", @"a relative link is left alone");
+ check(url(@"https://open.spotify.com/track/abc"), @"https://open.spotify.com/track/abc", @"nothing to take off");
+
+ check(SGCleanLinksInText(@"spotify:track:abc"), @"spotify:track:abc", @"a spotify: URI is left alone");
+ check(SGCleanLinksInText(@"just words"), @"just words", @"text with no link is left alone");
+ check(SGCleanLinksInText([track stringByAppendingString:@"?si=abc"]), track, @"a string that is a link");
+ check(SGCleanLinksInText(@"Listen: https://open.spotify.com/track/a?si=1&utm_source=copy-link and "
+ @"https://open.spotify.com/album/b?si=2&t=5."),
+ @"Listen: https://open.spotify.com/track/a and https://open.spotify.com/album/b?t=5.",
+ @"two links in a sentence, the full stop after the last kept");
+ check(SGCleanLinksInText(@"(https://open.spotify.com/track/a?si=1)"), @"(https://open.spotify.com/track/a)",
+ @"a closing bracket after a link is kept");
+ check(SGCleanLinksInText(@"https://example.com/x?si=1 https://open.spotify.com/track/a?si=1"),
+ @"https://example.com/x?si=1 https://open.spotify.com/track/a", @"only the Spotify link of two is cleaned");
+ check(SGCleanLinksInText(@"Écoute ça 🎧 https://open.spotify.com/track/a?si=1 👍"),
+ @"Écoute ça 🎧 https://open.spotify.com/track/a 👍", @"text around the link keeps its characters");
+
+ NSURL *object = SGCleanLinkObject([NSURL URLWithString:@"https://open.spotify.com/track/a?si=1"]);
+ check([object isKindOfClass:NSURL.class] ? object.absoluteString : @"not a URL", @"https://open.spotify.com/track/a",
+ @"an NSURL comes back an NSURL");
+ NSData *bytes = [@"x" dataUsingEncoding:NSUTF8StringEncoding];
+ check(SGCleanLinkObject(bytes) == bytes ? @"same" : @"changed", @"same", @"anything else comes back as it is");
+ }
+ printf("%d failed\n", failures);
+ return failures;
+}
diff --git a/harness/clean-links/shim/UIKit/UIKit.h b/harness/clean-links/shim/UIKit/UIKit.h
new file mode 100644
index 00000000..a50a87e5
--- /dev/null
+++ b/harness/clean-links/shim/UIKit/UIKit.h
@@ -0,0 +1,2 @@
+// Privacy.h's UIKit, on the Mac: the cleaner needs only Foundation.
+#import <Foundation/Foundation.h>
diff --git a/harness/clean-links/sim/main.m b/harness/clean-links/sim/main.m
new file mode 100644
index 00000000..e67eb98d
--- /dev/null
+++ b/harness/clean-links/sim/main.m
@@ -0,0 +1,90 @@
+// Checks CleanLinks.x's hooks in the iOS simulator: what reaches a pasteboard by each of its setters, and what
+// the share sheet's items answer, a provider's -item and an item source's answer included. Run by
+// ../build-sim.sh with simctl spawn, no app around it; the exit status is the number of failures.
+#import <UIKit/UIKit.h>
+
+static int failures;
+
+static void check(id got, NSString *wanted, NSString *what) {
+ NSString *text = [got isKindOfClass:NSURL.class] ? [got absoluteString] : [got description];
+ BOOL ok = [text isEqualToString:wanted];
+ printf("%s %s\n", ok ? "ok " : "FAIL", what.UTF8String);
+ if (!ok) printf(" got %s\n wanted %s\n", text.UTF8String, wanted.UTF8String);
+ failures += !ok;
+}
+
+static NSString *const kDirty = @"https://open.spotify.com/track/a?si=1&utm_source=copy-link";
+static NSString *const kClean = @"https://open.spotify.com/track/a";
+
+// Overrides -item the way a provider does, Branch's among them.
+@interface CheckProvider : UIActivityItemProvider
+@end
+@implementation CheckProvider
+- (id)item {
+ return [NSURL URLWithString:kDirty];
+}
+@end
+
+@interface CheckSubProvider : CheckProvider
+@end
+@implementation CheckSubProvider
+@end
+
+@interface CheckSource : NSObject <UIActivityItemSource>
+@end
+@implementation CheckSource
+- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)sheet {
+ return @"";
+}
+- (id)activityViewController:(UIActivityViewController *)sheet itemForActivityType:(UIActivityType)type {
+ return [@"Listen: " stringByAppendingString:kDirty];
+}
+@end
+
+int main(void) {
+ @autoreleasepool {
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:@"spotifyglass.cleanSharedLinks"];
+ UIPasteboard *board = [UIPasteboard pasteboardWithUniqueName];
+
+ board.string = kDirty;
+ check(board.string, kClean, @"setString:");
+ board.URL = [NSURL URLWithString:kDirty];
+ check(board.URL, kClean, @"setURL:");
+ board.strings = @[kDirty, @"plain"];
+ check([board.strings componentsJoinedByString:@" "], [kClean stringByAppendingString:@" plain"], @"setStrings:");
+ [board setItems:@[@{@"public.utf8-plain-text": kDirty}] options:@{}];
+ check(board.string, kClean, @"setItems:options: with a string");
+ board.items = @[@{@"public.url": [NSURL URLWithString:kDirty]}];
+ check(board.URL, kClean, @"setItems: with a URL");
+ [board setData:[kDirty dataUsingEncoding:NSUTF8StringEncoding] forPasteboardType:@"public.utf8-plain-text"];
+ check(board.string, kClean, @"setData:forPasteboardType: as text");
+ NSData *image = [@"https://open.spotify.com/x?si=1" dataUsingEncoding:NSUTF8StringEncoding];
+ [board setData:image forPasteboardType:@"public.png"];
+ check([[NSString alloc] initWithData:[board dataForPasteboardType:@"public.png"] encoding:NSUTF8StringEncoding],
+ @"https://open.spotify.com/x?si=1", @"setData:forPasteboardType: under an image type is left alone");
+ board.string = @"https://example.com/?si=1";
+ check(board.string, @"https://example.com/?si=1", @"another host is left alone");
+
+ CheckProvider *provider = [[CheckProvider alloc] initWithPlaceholderItem:@""];
+ CheckSubProvider *subProvider = [[CheckSubProvider alloc] initWithPlaceholderItem:@""];
+ CheckSource *source = [CheckSource new];
+ UIActivityViewController *sheet = [[UIActivityViewController alloc]
+ initWithActivityItems:@[kDirty, [NSURL URLWithString:kDirty], provider, subProvider, source] applicationActivities:nil];
+ NSArray *items = [sheet valueForKey:@"activityItems"];
+ check(items.count > 1 ? items[0] : nil, kClean, @"a share sheet string");
+ check(items.count > 1 ? items[1] : nil, kClean, @"a share sheet URL");
+ check(provider.item, kClean, @"a provider's -item");
+ check(subProvider.item, kClean, @"a provider's subclass's -item");
+ check([source activityViewController:sheet itemForActivityType:UIActivityTypeCopyToPasteboard],
+ [@"Listen: " stringByAppendingString:kClean], @"an item source's answer");
+
+ [NSUserDefaults.standardUserDefaults setBool:NO forKey:@"spotifyglass.cleanSharedLinks"];
+ board.string = kDirty;
+ check(board.string, kDirty, @"switched off: setString: as it came");
+ check(provider.item, kDirty, @"switched off: a provider's -item as it came");
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:@"spotifyglass.cleanSharedLinks"];
+ [UIPasteboard removePasteboardWithName:board.name];
+ }
+ printf("%d failed\n", failures);
+ return failures;
+}
diff --git a/tweak/Sources/Shared/Privacy/CleanLinks.m b/tweak/Sources/Shared/Privacy/CleanLinks.m
new file mode 100644
index 00000000..43dd4219
--- /dev/null
+++ b/tweak/Sources/Shared/Privacy/CleanLinks.m
@@ -0,0 +1,63 @@
+// Clean shared links: the tracking Spotify adds to the open.spotify.com links it copies and shares, taken off.
+// Foundation only, so it runs on whatever thread the share sheet asks on and in the Mac check
+// (harness/clean-links).
+//
+// What goes is what says who shared the link and from where: si, nd, pt, context and every utm_. What the link
+// opens stays: the path with its intl-xx/ segment, the fragment, and every other parameter in its order --
+// t, above all, the second a shared episode starts at.
+#import "Privacy.h"
+
+static NSString *const kHost = @"open.spotify.com";
+
+static BOOL isTracking(NSString *name) {
+ NSString *lower = name.lowercaseString;
+ return [lower isEqualToString:@"si"] || [lower isEqualToString:@"nd"] || [lower isEqualToString:@"pt"] ||
+ [lower isEqualToString:@"context"] || [lower hasPrefix:@"utm_"];
+}
+
+NSURL *SGCleanLinkURL(NSURL *url) {
+ // The host checked before it is compared: a nil one would compare as the same.
+ if (![url isKindOfClass:NSURL.class] || !url.host || [url.host caseInsensitiveCompare:kHost] != NSOrderedSame) return url;
+ NSURLComponents *parts = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
+ // The encoded items, so what is kept goes back byte for byte as it came.
+ NSArray<NSURLQueryItem *> *items = parts.percentEncodedQueryItems;
+ if (!items.count) return url;
+ NSMutableArray<NSURLQueryItem *> *kept = [NSMutableArray array];
+ for (NSURLQueryItem *item in items) {
+ if (!isTracking(item.name)) [kept addObject:item];
+ }
+ if (kept.count == items.count) return url;
+ // nil and not an empty list: an empty one leaves the "?" behind.
+ parts.percentEncodedQueryItems = kept.count ? kept : nil;
+ return parts.URL ?: url;
+}
+
+NSString *SGCleanLinksInText(NSString *text) {
+ if (![text isKindOfClass:NSString.class] || [text rangeOfString:kHost options:NSCaseInsensitiveSearch].location == NSNotFound) {
+ return text;
+ }
+ // The detector rather than a pattern of its own: it knows where a link in a sentence ends, so a full stop
+ // or a closing bracket after it is not read as the end of the last parameter and taken off with it.
+ static NSDataDetector *detector;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:NULL]; });
+ NSArray<NSTextCheckingResult *> *matches = [detector matchesInString:text options:0 range:NSMakeRange(0, text.length)];
+ NSMutableString *cleaned = nil;
+ // From the end, so the ranges still to come are where they were.
+ for (NSTextCheckingResult *match in matches.reverseObjectEnumerator) {
+ // The link as written, not the detector's reading of it, which puts a scheme on a bare host.
+ NSString *written = [text substringWithRange:match.range];
+ NSURL *url = [NSURL URLWithString:written];
+ NSURL *clean = SGCleanLinkURL(url);
+ if (!url || clean == url) continue;
+ if (!cleaned) cleaned = [text mutableCopy];
+ [cleaned replaceCharactersInRange:match.range withString:clean.absoluteString];
+ }
+ return cleaned ? [cleaned copy] : text;
+}
+
+id SGCleanLinkObject(id object) {
+ if ([object isKindOfClass:NSURL.class]) return SGCleanLinkURL(object);
+ if ([object isKindOfClass:NSString.class]) return SGCleanLinksInText(object);
+ return object;
+}
diff --git a/tweak/Sources/Shared/Privacy/CleanLinks.x b/tweak/Sources/Shared/Privacy/CleanLinks.x
new file mode 100644
index 00000000..05fd5156
--- /dev/null
+++ b/tweak/Sources/Shared/Privacy/CleanLinks.x
@@ -0,0 +1,171 @@
+// Clean shared links (CleanLinks.m): every open.spotify.com link the app puts on the pasteboard or hands to the
+// share sheet goes out without its tracking.
+//
+// Spotify's binary sends -setString:, -setURL:, -setItems:, -setItems:options: and -setData:forPasteboardType:
+// to the pasteboard, and builds its share sheet with -initWithActivityItems:applicationActivities: and a
+// UIActivityItemProvider of Branch's among others. Which of them "Copy link" takes has not been seen on a
+// device, so each way onto the pasteboard is cleaned, the object ones too. Cleaning twice changes nothing, so
+// one of them calling another is harmless.
+//
+// The pasteboard is hooked on _UIConcretePasteboard, not UIPasteboard: every pasteboard UIKit hands out is one,
+// and it overrides each setter, so a hook on UIPasteboard's own never ran (simulator, iOS 27).
+//
+// The switch is read on each copy and share, so it needs no restart. Anything that is not an open.spotify.com
+// link -- the mod's own backups and signing pages, images, spotify: URIs -- goes through as it came.
+#import "Core/SGCore.h"
+#import "Privacy.h"
+
+static BOOL cleaning(void) {
+ return SGEnabled(SGKeyCleanLinks);
+}
+
+static NSArray *cleanAll(NSArray *objects) {
+ if (![objects isKindOfClass:NSArray.class]) return objects;
+ NSMutableArray *cleaned = [NSMutableArray arrayWithCapacity:objects.count];
+ for (id object in objects) [cleaned addObject:SGCleanLinkObject(object)];
+ return cleaned;
+}
+
+// Bytes are only read as text under a type that is text or a link; an image is left alone.
+static BOOL isTextType(NSString *type) {
+ static NSSet<NSString *> *types;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ types = [NSSet setWithArray:@[@"public.url", @"public.text", @"public.plain-text", @"public.utf8-plain-text"]];
+ });
+ return [types containsObject:type];
+}
+
+static id cleanValue(id value, NSString *type) {
+ if (![value isKindOfClass:NSData.class]) return SGCleanLinkObject(value);
+ if (!isTextType(type)) return value;
+ NSString *text = [[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding];
+ NSString *cleaned = SGCleanLinksInText(text);
+ return cleaned == text ? value : [cleaned dataUsingEncoding:NSUTF8StringEncoding];
+}
+
+static NSArray<NSDictionary *> *cleanItems(NSArray<NSDictionary *> *items) {
+ if (![items isKindOfClass:NSArray.class]) return items;
+ NSMutableArray *cleaned = [NSMutableArray arrayWithCapacity:items.count];
+ for (NSDictionary *item in items) {
+ if (![item isKindOfClass:NSDictionary.class]) {
+ [cleaned addObject:item];
+ continue;
+ }
+ NSMutableDictionary *copy = [NSMutableDictionary dictionaryWithCapacity:item.count];
+ [item enumerateKeysAndObjectsUsingBlock:^(id type, id value, BOOL *stop) {
+ copy[type] = cleanValue(value, type);
+ }];
+ [cleaned addObject:copy];
+ }
+ return cleaned;
+}
+
+%hook _UIConcretePasteboard
+- (void)setString:(NSString *)string {
+ %orig(cleaning() ? SGCleanLinksInText(string) : string);
+}
+- (void)setStrings:(NSArray<NSString *> *)strings {
+ %orig(cleaning() ? cleanAll(strings) : strings);
+}
+- (void)setURL:(NSURL *)url {
+ %orig(cleaning() ? SGCleanLinkURL(url) : url);
+}
+- (void)setURLs:(NSArray<NSURL *> *)urls {
+ %orig(cleaning() ? cleanAll(urls) : urls);
+}
+- (void)setObjects:(NSArray *)objects {
+ %orig(cleaning() ? cleanAll(objects) : objects);
+}
+- (void)setObjects:(NSArray *)objects localOnly:(BOOL)localOnly expirationDate:(NSDate *)date {
+ %orig(cleaning() ? cleanAll(objects) : objects, localOnly, date);
+}
+- (void)setObjects:(NSArray *)objects options:(NSDictionary *)options {
+ %orig(cleaning() ? cleanAll(objects) : objects, options);
+}
+- (void)setItems:(NSArray<NSDictionary *> *)items {
+ %orig(cleaning() ? cleanItems(items) : items);
+}
+- (void)setItems:(NSArray<NSDictionary *> *)items options:(NSDictionary *)options {
+ %orig(cleaning() ? cleanItems(items) : items, options);
+}
+- (void)addItems:(NSArray<NSDictionary *> *)items {
+ %orig(cleaning() ? cleanItems(items) : items);
+}
+- (void)setValue:(id)value forPasteboardType:(NSString *)type {
+ %orig(cleaning() ? cleanValue(value, type) : value, type);
+}
+- (void)setData:(NSData *)data forPasteboardType:(NSString *)type {
+ %orig(cleaning() ? cleanValue(data, type) : data, type);
+}
+%end
+
+#pragma mark - the share sheet
+
+// The method `cls` answers `selector` with, its own or inherited, made to clean what it returns. A provider
+// overrides -item rather than the base class answering, so a hook on UIActivityItemProvider would never run:
+// each class that reaches the sheet is taken as it comes, once.
+static void cleanReturn(Class cls, SEL selector, id (^make)(IMP original)) {
+ Method inherited = class_getInstanceMethod(cls, selector);
+ if (!inherited) return;
+ Method own = nil;
+ unsigned int count = 0;
+ Method *methods = class_copyMethodList(cls, &count);
+ for (unsigned int i = 0; i < count && !own; i++) {
+ if (method_getName(methods[i]) == selector) own = methods[i];
+ }
+ free(methods);
+ IMP original = own ? method_getImplementation(own) : class_getMethodImplementation(class_getSuperclass(cls), selector);
+ if (!original) return;
+ IMP cleaned = imp_implementationWithBlock(make(original));
+ if (own) method_setImplementation(own, cleaned);
+ else class_addMethod(cls, selector, cleaned, method_getTypeEncoding(inherited));
+}
+
+static void cleanSource(id item) {
+ static NSMutableSet<Class> *taken;
+ if (!taken) taken = [NSMutableSet set];
+ Class cls = object_getClass(item);
+ if (!cls || [taken containsObject:cls]) return;
+ [taken addObject:cls];
+
+ SEL itemFor = @selector(activityViewController:itemForActivityType:);
+ cleanReturn(cls, itemFor, ^id(IMP original) {
+ return ^id(id me, UIActivityViewController *sheet, UIActivityType type) {
+ id value = ((id (*)(id, SEL, UIActivityViewController *, UIActivityType))original)(me, itemFor, sheet, type);
+ return cleaning() ? SGCleanLinkObject(value) : value;
+ };
+ });
+ // A provider's -item runs on a queue of the sheet's, not the main thread: the cleaning is Foundation only.
+ if ([item isKindOfClass:UIActivityItemProvider.class]) {
+ SEL itemSel = @selector(item);
+ cleanReturn(cls, itemSel, ^id(IMP original) {
+ return ^id(id me) {
+ id value = ((id (*)(id, SEL))original)(me, itemSel);
+ return cleaning() ? SGCleanLinkObject(value) : value;
+ };
+ });
+ }
+ SGLog(@"privacy: share sheet items of %@ cleaned", NSStringFromClass(cls));
+}
+
+%hook UIActivityViewController
+- (instancetype)initWithActivityItems:(NSArray *)items applicationActivities:(NSArray<UIActivity *> *)activities {
+ if (!cleaning() || ![items isKindOfClass:NSArray.class]) return %orig;
+ NSMutableArray *cleaned = [NSMutableArray arrayWithCapacity:items.count];
+ for (id item in items) {
+ if ([item isKindOfClass:NSString.class] || [item isKindOfClass:NSURL.class]) {
+ [cleaned addObject:SGCleanLinkObject(item)];
+ continue;
+ }
+ if ([item conformsToProtocol:@protocol(UIActivityItemSource)]) cleanSource(item);
+ [cleaned addObject:item];
+ }
+ return %orig(cleaned, activities);
+}
+%end
+
+%ctor {
+ %init;
+ SGRequireClasses(@[@"_UIConcretePasteboard"]);
+}
diff --git a/tweak/Sources/Shared/Privacy/Privacy.h b/tweak/Sources/Shared/Privacy/Privacy.h
index f176a3af..31f74269 100644
--- a/tweak/Sources/Shared/Privacy/Privacy.h
+++ b/tweak/Sources/Shared/Privacy/Privacy.h
@@ -1,9 +1,20 @@
// Privacy: telemetry blocking (Privacy.x), an NSURLProtocol that answers the analytics endpoints
-// itself and counts what it stopped. On unless switched off.
+// itself and counts what it stopped, and clean shared links. Both on unless switched off.
#import <UIKit/UIKit.h>
#define SGKeyBlockTelemetry @"spotifyglass.blockTelemetry"
+// Clean shared links (CleanLinks.m, CleanLinks.x): the tracking parameters taken off the open.spotify.com links
+// the app copies and shares. On unless switched off, read on each copy.
+#define SGKeyCleanLinks @"spotifyglass.cleanSharedLinks"
+// The link without si, nd, pt, context and utm_*, or `url` itself when it is not an open.spotify.com link,
+// does not parse, or has none of them.
+NSURL *SGCleanLinkURL(NSURL *url);
+// `text` with every open.spotify.com link in it cleaned in place, or `text` itself when there was nothing to clean.
+NSString *SGCleanLinksInText(NSString *text);
+// Either of the two by the object's class; anything else comes back as it is.
+id SGCleanLinkObject(id object);
+
// The destinations it knows in the order it lists them, and how many requests to one of them it
// has answered instead of letting out (nil label for all of them).
NSArray<NSString *> *SGBlockedLabels(void);
@@ -11,6 +22,7 @@ NSUInteger SGBlockedCount(NSString *label);
void SGResetBlocked(void);
@class SGModSection;
-// The telemetry switch and what it has stopped, on the Premium, ads & privacy page.
+// The telemetry and shared links switches and what the telemetry blocking has stopped, on the
+// Premium, ads & privacy page.
SGModSection *SGPrivacySection(void);
SGModSection *SGPrivacyCountersSection(void);
diff --git a/tweak/Sources/Shared/Privacy/PrivacySettings.m b/tweak/Sources/Shared/Privacy/PrivacySettings.m
index 599eebfa..d0462c67 100644
--- a/tweak/Sources/Shared/Privacy/PrivacySettings.m
+++ b/tweak/Sources/Shared/Privacy/PrivacySettings.m
@@ -4,6 +4,7 @@
SGModSection *SGPrivacySection(void) {
return SGSection(@"Privacy", @[
SGWithSymbol(SGSwitchRow(@"Block telemetry", @"Spotify's own events still go out, since Recents is built from them", SGKeyBlockTelemetry), @"antenna.radiowaves.left.and.right.slash"),
+ SGWithSymbol(SGSwitchRow(@"Clean shared links", @"Takes the tracking (si, utm) off the links you copy or share", SGKeyCleanLinks), @"link"),
]);
}