Skip to content

Instantly share code, notes, and snippets.

@zero-mstd
Last active November 12, 2022 16:33
Show Gist options
  • Save zero-mstd/f7a007c2a6d53d804883d75b9e372228 to your computer and use it in GitHub Desktop.
Save zero-mstd/f7a007c2a6d53d804883d75b9e372228 to your computer and use it in GitHub Desktop.
/*
* Usage:
* 1. Open your instance in a web browser and log in with your account;
* 2. Press `F12` to open the console in your browser’s developer tools;
* 3. Copy all the code here and paste them on the console;
* 4. Hit `ENTER` and enjoy your time!
*
* Known Issue:
* The process may reach Mastodon’s limit. In that case, you can try again
* after a few minutes of rest.
*/
const host = document.location.hostname;
const token = JSON.parse(document.querySelector('#initial-state').text).meta.access_token;
var get_url = 'https://' + host + '/api/v1/bookmarks';
async function fetchBM(url) {
console.log('A new page of bookmarks have been found!');
var headers;
var bmPromise = fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + token,
}});
// use await here so that the `headers` variable is defined properly.
// See, https://stackoverflow.com/a/14220323
await bmPromise
.then( response => {
headers = response.headers;
return response.json();
})
.then( result => {
for (obj of result) {
var id = obj['id'];
var ub_url = 'https://' + host + '/api/v1/statuses/' + id + '/unbookmark';
fetch(ub_url, {
method: 'POST',
headers: {
Authorization: 'Bearer ' + token,
}}).then(() => {
console.log('Unbookmarked ' + id);
});
}
});
return headers;
}
(async function loop() {
while(1) {
var h = await fetchBM(get_url);
if (!(h.has('link') && h.get('link').includes('rel=\"next\"'))) {
break;
} else {
get_url = h.get('link').match(/\<([^>]+?)\>/).pop();
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment