Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Licensed as Open Source under MIT. Full License: https://pastebin.com/gwqXTTLY
- getAccessToken = () => {
- let token;
- try{
- token = JSON.parse(localStorage.LOGGED_IN_USER).accessToken;
- }
- catch(err){
- throw "Could not get access token. Are you logged in?"
- }
- if(!token){
- throw "Empty token."
- }
- return token;
- };
- runMutation = async(accessToken, publicID, mutationName) => {
- return fetch("https://api.aidungeon.io/graphql", {
- "headers": {
- "accept": "*/*",
- "cache-control": "no-cache",
- "content-type": "application/json",
- "pragma": "no-cache",
- "x-access-token": accessToken
- },
- "referrer": "https://play.aidungeon.io/",
- "referrerPolicy": "strict-origin-when-cross-origin",
- "body": JSON.stringify({
- query: `mutation ($publicId: String) {
- ${mutationName}(publicId: $publicId) {
- id
- publicId
- deletedAt
- isSaved
- __typename
- }
- }`,
- variables: {
- publicId: publicID
- }
- }),
- "method": "POST",
- "mode": "cors",
- "credentials": "omit"
- });
- }
- deleteAdventure = async (accessToken, publicID) => {
- console.log(`Deleting Adventure: ${publicID}`);
- return runMutation(accessToken, publicID, "deleteAdventure");
- }
- restoreAdventure = async (accessToken, publicID) => {
- console.log(`Restoring Adventure: ${publicID}`);
- return runMutation(accessToken, publicID, "restoreAdventure");
- }
- deleteScenario = async (accessToken, publicID) => {
- console.log(`Deleting Scenario: ${publicID}`);
- return runMutation(accessToken, publicID, "deleteScenario");
- }
- restoreScenario = async (accessToken, publicID) => {
- console.log(`Restoring Scenario: ${publicID}`);
- return runMutation(accessToken, publicID, "restoreScenario");
- }
- deletePost = async (accessToken, publicID) => {
- console.log(`Deleting Post: ${publicID}`);
- return runMutation(accessToken, publicID, "deletePost");
- }
- restorePost = async (accessToken, publicID) => {
- console.log(`Restoring Post: ${publicID}`);
- return runMutation(accessToken, publicID, "restorePost");
- }
- emptyTrash = () => {
- return fetch("https://api.aidungeon.io/graphql", {
- "headers": {
- "accept": "*/*",
- "cache-control": "no-cache",
- "content-type": "application/json",
- "pragma": "no-cache",
- "x-access-token": accessToken
- },
- "referrer": "https://play.aidungeon.io/",
- "referrerPolicy": "strict-origin-when-cross-origin",
- "body": JSON.stringify({
- query: `mutation {
- emptyTrash
- }`,
- variables: {}
- }),
- "method": "POST",
- "mode": "cors",
- "credentials": "omit"
- });
- }
- fetchContent = async (accessToken, contentType, checkTrash) => {
- const r = await fetch("https://api.aidungeon.io/graphql", {
- "headers": {
- "accept": "*/*",
- "accept-language": "en-US,en;q=0.9",
- "cache-control": "no-cache",
- "content-type": "application/json",
- "pragma": "no-cache",
- "x-access-token": accessToken
- },
- "referrer": "https://play.aidungeon.io/",
- "referrerPolicy": "strict-origin-when-cross-origin",
- "body": JSON.stringify({
- query: `query ($input: SearchInput) {
- user {
- id
- search(input: $input) {
- publicId
- __typename
- }
- __typename
- }
- }`,
- variables: {
- input: {
- contentType: contentType,
- offset: 0,
- saved: false,
- searchTerm: "",
- sortOrder: "actionCount",
- trash: contentType === "trash" || checkTrash
- }
- }
- }),
- "method": "POST",
- "mode": "cors",
- "credentials": "omit"
- })
- return r.json();
- };
- // data.adventure.actions is an array of objects where the text field is the story data. You'll have to pull that together.
- sleep = (ms) => {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- deleteItemsOnFirstPage = async (accessToken, contentType) => {
- console.log(`Checking for content...`)
- r = await fetchContent(accessToken, contentType);
- if(r.data.user.search.length > 0){
- await Promise.all(r.data.user.search.map(
- ea => {
- __typename = ea.__typename.toLowerCase();
- if(__typename === "scenario"){
- deleteScenario(accessToken, ea.publicId)
- }
- else if(__typename === "adventure"){
- deleteAdventure(accessToken, ea.publicId)
- }
- else if(__typename === "post"){
- deletePost(accessToken, ea.publicId)
- }
- }
- ))
- return true
- }
- else{
- return false
- }
- }
- restoreItemsOnFirstPage = async (accessToken, contentTypes) => {
- console.log(`Restoring trash...`)
- items = await getTrash(accessToken, contentTypes);
- if(items.length > 0){
- await Promise.all(items.map(
- ea => {
- __typename = ea.__typename.toLowerCase();
- if(__typename === "scenario"){
- restoreScenario(accessToken, ea.publicId)
- }
- else if(__typename === "adventure"){
- restoreAdventure(accessToken, ea.publicId)
- }
- else if(__typename === "post"){
- restorePost(accessToken, ea.publicId)
- }
- }
- ))
- return true
- }
- else{
- return false
- }
- }
- restoreTrash = async (contentTypes = ["scenario","adventure", "post"]) => {
- accessToken = getAccessToken();
- hasMore = true;
- console.log("restoring")
- while(hasMore){
- hasMore = await restoreItemsOnFirstPage(accessToken, contentTypes);
- console.log("Waiting 1.5 seconds...")
- await sleep(1500);
- if(!hasMore){
- console.log("Done. Content Restored.")
- }
- }
- }
- deleteAllContent = async (contentType) => {
- accessToken = getAccessToken();
- hasMore = true;
- console.log(`Initiating Delete of all ${contentType}`)
- while(hasMore){
- hasMore = await deleteItemsOnFirstPage(accessToken, contentType);
- console.log("Waiting 1.5 seconds...")
- await sleep(1500);
- if(!hasMore){
- console.log(`Done. Deleted all ${contentType}`)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment