Created
January 8, 2019 16:11
-
-
Save hieuhuynh93/6a5af6e9582794943f6a18abb7a026be to your computer and use it in GitHub Desktop.
Unclap everything on Medium
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
/** | |
* Unclaps everything on Medium | |
* @author Jane Manchun Wong | |
*/ | |
(async () => { | |
if ( window.location.host !== 'medium.com' ) { | |
console.error(`This script won't work unless you go to Medium:\nhttps://medium.com/me`) | |
return | |
} | |
/** | |
* Send Medium server POST requests | |
* @param input {string} URI | |
* @param payload {Object} JSON Payload | |
* @returns {Object} Result | |
*/ | |
const _post = async (input, payload) => { | |
const res = await fetch(input, { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'x-xsrf-token': 1 | |
}, | |
body: JSON.stringify(payload) | |
}) | |
const content = await res.text() | |
return JSON.parse(content.replace(/\]\)}while\(1\);<\/x>/,'')) | |
} | |
const { | |
data: { | |
viewer: { | |
userId, | |
clapsStream: { | |
nodes: clapNodes | |
} | |
} | |
} | |
} = await _post('/_/graphql', { | |
query: `query { | |
viewer { | |
userId: id | |
clapsStream: clapsStreamConnection(paging:{limit:10000}) { | |
nodes: stream { | |
node: itemType { | |
... on StreamItemPostPreview { | |
post { | |
id | |
viewerClapCount | |
title | |
} | |
} | |
} | |
} | |
} | |
} | |
}` | |
}) | |
if(clapNodes.length === 0) { | |
console.log(`No claps found on your profile. You are good to go!`) | |
return; | |
} | |
clapNodes.forEach(({ node: { post } }, i, { length }) => { | |
(async () => { | |
if (!post) { | |
console.log(`${i+1}/${length} skipped`) | |
return | |
} | |
const { success } = await _post(`/_/api/posts/${post.id}/claps`, { | |
userId, | |
clapIncrement: -post.viewerClapCount | |
}) | |
if (success) console.log(`✅ ${i+1}/${length} unclapped: ${post.id} - ${post.title}`) | |
else console.error(`Unable to unclap post:${post.id}`) | |
})() | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment