-
Notifications
You must be signed in to change notification settings - Fork 70
/
adblock.js
206 lines (206 loc) · 8.42 KB
/
adblock.js
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
"use strict";
const loadWebpack = () => {
try {
const require = window.webpackChunkclient_web.push([[Symbol()], {}, (re) => re]);
const cache = Object.keys(require.m).map(id => require(id));
const modules = cache
.filter(module => typeof module === "object")
.flatMap(module => {
try {
return Object.values(module);
}
catch { }
});
const functionModules = modules.filter(module => typeof module === "function");
return { cache, functionModules };
}
catch (error) {
console.error("adblockify: Failed to load webpack", error);
return { cache: [], functionModules: [] };
}
};
const getSettingsClient = (cache) => {
try {
return cache.find((m) => m.settingsClient).settingsClient;
}
catch (error) {
console.error("adblockify: Failed to get ads settings client", error);
return null;
}
};
const getSlotsClient = (functionModules, transport) => {
try {
const slots = functionModules.find(m => m.SERVICE_ID === "spotify.ads.esperanto.slots.proto.Slots" || m.SERVICE_ID === "spotify.ads.esperanto.proto.Slots");
return new slots(transport);
}
catch (error) {
console.error("adblockify: Failed to get slots client", error);
return null;
}
};
const map = new Map();
const retryCounter = (slotId, action) => {
if (!map.has(slotId))
map.set(slotId, { count: 0 });
if (action === "increment")
map.get(slotId).count++;
else if (action === "clear")
map.delete(slotId);
else if (action === "get")
return map.get(slotId)?.count;
};
(async function adblockify() {
// @ts-expect-error: Events are not defined in types
await new Promise(res => Spicetify.Events.platformLoaded.on(res));
if (!window.webpackChunkclient_web) {
setTimeout(adblockify, 50);
return;
}
const webpackCache = loadWebpack();
// @ts-expect-error: expFeatureOverride is not defined in types
const { CosmosAsync, Platform, expFeatureOverride, Locale } = Spicetify;
const { AdManagers } = Platform;
const { audio } = AdManagers;
const { UserAPI } = Platform;
const productState = UserAPI._product_state || UserAPI._product_state_service || Platform?.ProductStateAPI?.productStateApi;
if (!CosmosAsync) {
setTimeout(adblockify, 100);
return;
}
const slots = await CosmosAsync.get("sp://ads/v1/slots");
const disableAds = async () => {
try {
await productState.putOverridesValues({ pairs: { ads: "0", catalogue: "premium", product: "premium", type: "premium" } });
}
catch (error) {
console.error("adblockify: Failed inside `disableAds` function\n", error);
}
};
const configureAdManagers = async () => {
try {
const { billboard, leaderboard, inStreamApi, sponsoredPlaylist } = AdManagers;
audio.audioApi.cosmosConnector.increaseStreamTime(-100000000000);
billboard.billboardApi.cosmosConnector.increaseStreamTime(-100000000000);
await audio.disable();
audio.isNewAdsNpvEnabled = false;
await billboard.disable();
await leaderboard.disableLeaderboard();
await inStreamApi.disable();
await sponsoredPlaylist.disable();
if (AdManagers?.vto) {
const { vto } = AdManagers;
await vto.manager.disable();
vto.isNewAdsNpvEnabled = false;
}
setTimeout(disableAds, 100);
}
catch (error) {
console.error("adblockify: Failed inside `configureAdManagers` function\n", error);
}
};
const bindToSlots = async () => {
for (const slot of slots) {
subToSlot(slot.slot_id);
handleAdSlot({ adSlotEvent: { slotId: slot.slot_id } });
}
};
const handleAdSlot = (data) => {
const slotId = data?.adSlotEvent?.slotId;
try {
const adsCoreConnector = audio.inStreamApi.adsCoreConnector;
if (typeof adsCoreConnector?.clearSlot === "function")
adsCoreConnector.clearSlot(slotId);
const slotsClient = getSlotsClient(webpackCache.functionModules, productState.transport);
if (slotsClient)
slotsClient.clearAllAds({ slotId });
updateSlotSettings(slotId);
}
catch (error) {
console.error("adblockify: Failed inside `handleAdSlot` function. Retrying in 1 second...\n", error);
retryCounter(slotId, "increment");
if (retryCounter(slotId, "get") > 5) {
console.error(`adblockify: Failed inside \`handleAdSlot\` function for 5th time. Giving up...\nSlot id: ${slotId}.`);
retryCounter(slotId, "clear");
return;
}
setTimeout(handleAdSlot, 1 * 1000, data);
}
configureAdManagers();
};
const updateSlotSettings = async (slotId) => {
try {
const settingsClient = getSettingsClient(webpackCache.cache);
if (!settingsClient)
return;
await settingsClient.updateStreamTimeInterval({ slotId, timeInterval: "0" });
await settingsClient.updateSlotEnabled({ slotId, enabled: false });
await settingsClient.updateDisplayTimeInterval({ slotId, timeInterval: "0" });
}
catch (error) {
console.error("adblockify: Failed inside `updateSlotSettings` function\n", error);
}
};
const intervalUpdateSlotSettings = async () => {
for (const slot of slots) {
updateSlotSettings(slot.slot_id);
}
};
const subToSlot = (slot) => {
try {
audio.inStreamApi.adsCoreConnector.subscribeToSlot(slot, handleAdSlot);
}
catch (error) {
console.error("adblockify: Failed inside `subToSlot` function\n", error);
}
};
const runObserver = () => {
const nodeList = Array.from(document.querySelectorAll(".ReactModalPortal"));
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
const node = mutation.addedNodes[0];
const InAppModal = node.classList.contains("GenericModal__overlay");
if (!InAppModal)
continue;
const iframe = node.querySelector("iframe");
if (!iframe)
continue;
const iframeBody = iframe?.contentWindow?.document.body;
if (!iframeBody)
continue;
const promotional = iframeBody.querySelector("[data-click-to-action-url*='/premium-promotional-offer-terms']");
if (!promotional)
continue;
node.remove();
}
}
});
for (const node of nodeList) {
observer.observe(node, { childList: true });
}
};
const enableExperimentalFeatures = async () => {
try {
const expFeatures = JSON.parse(localStorage.getItem("spicetify-exp-features") || "{}");
const hptoEsperanto = expFeatures.enableEsperantoMigration?.value;
const inAppMessages = expFeatures.enableInAppMessaging?.value;
const upgradeCTA = expFeatures.hideUpgradeCTA?.value;
if (!hptoEsperanto)
expFeatureOverride({ type: "bool", name: "enableEsperantoMigration", default: true });
if (inAppMessages)
expFeatureOverride({ type: "bool", name: "enableInAppMessaging", default: false });
if (!upgradeCTA)
expFeatureOverride({ type: "bool", name: "hideUpgradeCTA", default: true });
}
catch (error) {
console.error("adblockify: Failed inside `enableExperimentalFeatures` function\n", error);
}
};
enableExperimentalFeatures();
bindToSlots();
// to enable one day if disabling `enableInAppMessages` exp feature doesn't work
//runObserver();
productState.subValues({ keys: ["ads", "catalogue", "product", "type"] }, () => configureAdManagers());
// Update slot settings after 5 seconds... idk why, don't ask me why, it just works
setTimeout(intervalUpdateSlotSettings, 5 * 1000);
})();