Skip to content

Instantly share code, notes, and snippets.

@Venipa
Last active July 28, 2020 22:12
Show Gist options
  • Select an option

  • Save Venipa/69c3bc60193004bfdd652d9a5c83559c to your computer and use it in GitHub Desktop.

Select an option

Save Venipa/69c3bc60193004bfdd652d9a5c83559c to your computer and use it in GitHub Desktop.
Unsubscribe every tag in https://wallhaven.cc/subscription
class Queue {
static queue = [];
static pendingPromise = false;
static enqueue(promise) {
return new Promise((resolve, reject) => {
this.queue.push({
promise,
resolve,
reject,
});
this.dequeue();
});
}
static dequeue() {
if (this.workingOnPromise) {
return false;
}
const item = this.queue.shift();
if (!item) {
return false;
}
try {
this.workingOnPromise = true;
item
.promise()
.then((value) => {
this.workingOnPromise = false;
item.resolve(value);
this.dequeue();
})
.catch((err) => {
this.workingOnPromise = false;
item.reject(err);
this.dequeue();
});
} catch (err) {
this.workingOnPromise = false;
item.reject(err);
this.dequeue();
}
return true;
}
}
(function () {
const subs = document.querySelectorAll(".subscription-list a");
const subLinks = Array.from(subs);
const subids = subLinks.map((x) => ({ name: x.textContent, id: /(.*)\/tag\/(.+)/g.exec(x.href)[2] }));
const getCSRF = () =>
document.querySelector('meta[name="csrf-token"]').content;
if (subids.length === 0) return;
subids.forEach((x) => {
Queue.enqueue(
() =>
new Promise((resolve, reject) => {
return setTimeout(() => {
$.ajax({
url: `https://wallhaven.cc/subscription/remove/tag/${x.id}?_token=${getCSRF()}`,
success: function () {
resolve();
console.log(`tag ${x.name} has been unsubscribed`);
},
error: function () {
reject();
console.log(`tag ${x.name} returned an error while unsubscribing (try again later once done)`);
},
});
}, 2000);
})
);
});
})();
@Venipa
Copy link
Copy Markdown
Author

Venipa commented Jul 28, 2020

added delay due to ratelimit

@Venipa
Copy link
Copy Markdown
Author

Venipa commented Jul 28, 2020

console logs added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment