Last active
June 14, 2023 13:22
-
-
Save yorunoken/494d9e1105c5040506168751a9bee8c0 to your computer and use it in GitHub Desktop.
A bookmarklet I wrote to automatically transfer Lemmy subscriptions to other instances.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: (async function () { | |
const currentURL = window.location.href; | |
const currentHostname = window.location.hostname; | |
if (!currentURL.includes("listing_type/Subscribed/page")) { | |
window.alert("Not in the current page. Redirecting.."); | |
window.location.href = `communities/listing_type/Subscribed/page/1`; | |
} | |
const table = document.getElementById("community_table"); | |
const anchorTags = table.getElementsByTagName("a"); | |
const titles = []; | |
for (let i = 0; i < anchorTags.length; i++) { | |
let title = anchorTags[i].title.substring(1); | |
if (!title.includes("@")) { | |
title += `@${currentHostname}`; | |
} | |
titles.push(title); | |
} | |
var instancePrompt = window.prompt("Please enter what instance you want to add the bookmarks to: (lemmy.ml, lemmy.world, beehaw.org, etc.)", ""); | |
if (!instancePrompt) { | |
alert("Please input your preferred instance!"); | |
return; | |
} | |
var emailInput = window.prompt("Please enter your username or email:", ""); | |
if (!emailInput) { | |
alert("Please input your email or email!"); | |
return; | |
} | |
var passwordInput = window.prompt("Please enter your password:", ""); | |
if (!passwordInput) { | |
alert("Please input your password!"); | |
return; | |
} | |
const baseURL = `https://cors-anywhere.herokuapp.com/https://${instancePrompt.toLowerCase()}/api/v3`; | |
let loginForm = { | |
username_or_email: emailInput, | |
password: passwordInput, | |
}; | |
try { | |
var jwt = await fetch(`${baseURL}/user/login`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
"access-control-allow-origin": "*", | |
}, | |
body: JSON.stringify(loginForm), | |
}); | |
} catch (err) { | |
console.log(err); | |
alert("Visit https://cors-anywhere.herokuapp.com/corsdemo and click on 'participate in demo' then try again"); | |
return; | |
} | |
if (jwt.ok === false) { | |
if (jwt.status === 429) { | |
alert("Too many requests from this IP, try again."); | |
return; | |
} | |
jwt = await jwt.json(); | |
if (jwt.error === "no t_logged_in") { | |
alert("Invalid login, please make sure your credentials are correct and try again."); | |
return; | |
} | |
} else { | |
jwt = await jwt.json(); | |
jwt = jwt.jwt; | |
} | |
let failedRequests = []; | |
for (const community of titles) { | |
const postForm = { | |
auth: jwt, | |
name: community, | |
}; | |
const queryParams = new URLSearchParams(postForm); | |
let requestedCommunity = await fetch(`${baseURL}/community?${queryParams}`, { | |
method: "GET", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
}); | |
if (!requestedCommunity.statusText === "OK") { | |
failedRequests.push(community); | |
continue; | |
} | |
requestedCommunity = await requestedCommunity.json(); | |
const communityID = requestedCommunity?.community_view?.community?.id; | |
const subscribeForm = { | |
auth: jwt, | |
community_id: communityID, | |
follow: true, | |
}; | |
if (!communityID) { | |
fetch(`${baseURL}/community/follow`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify(subscribeForm), | |
}).then(() => console.log(`subscribed to ${requestedCommunity?.community_view?.community?.name} in ${instancePrompt}`)); | |
} | |
} | |
if (failedRequests.length > 0) { | |
alert(`Done, but some of the communities gave errors, most likely because they've been blocked by your instance, here's a list of them:\n${failedRequests.join("\n")}`); | |
} else { | |
alert("Done!"); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment