Created
September 6, 2018 00:00
-
-
Save MatthiasPortzel/86c0e549efdd24d49e04102678407167 to your computer and use it in GitHub Desktop.
KA voting script, this time without down-voting, hopefully for the KA extension.
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
(function () { | |
/* ---- Definitions and fetching information ---- */ | |
const EXTENSION_ITEM_CLASSNAME = "m-better-voting"; | |
let oldWrap = document.querySelector(".voting-wrap .discussion-meta-controls").firstChild; | |
//No voting wrap means we're looking at a child account program or a tutorial/offical video or program, contest, etc | |
if (!oldWrap || !oldWrap.innerText.includes("Vote")) { | |
return; | |
} | |
const unvotedClass = "link_1uvuyao-o_O-computing_77ub1h"; | |
const votedClass = "link_1uvuyao-o_O-computing_1w8n1i8"; | |
const attr = KAdefine.require("./javascript/scratchpads-package/scratchpad-ui.js").ScratchpadUI.scratchpad.attributes; | |
const cookie = KAdefine.require("./javascript/shared-package/cookies.js").readCookie("fkey"); //getCSRF() | |
let voted = oldWrap.innerText.includes("Voted Up"); //If we've voted | |
const orgVotes = attr.sumVotesIncremented - (voted ? 1 : 0); //Number of votes not counting ours | |
/* ---- Defining Custom Behavior ---- */ | |
let newWrap = document.createElement("span"); | |
newWrap.classList.add("voting-wrap"); | |
newWrap.innerHTML = ` | |
<a class="${unvotedClass} ${EXTENSION_ITEM_CLASSNAME}" id="m-vote-button"> | |
<span id="m-vote-text">Vote Up</span> • <span id="m-vote-count">${orgVotes}</span> | |
</a> | |
`; | |
const voteCount = newWrap.querySelector("#m-vote-count"); | |
const voteButton = newWrap.querySelector("#m-vote-button"); | |
const voteText = newWrap.querySelector("#m-vote-text"); | |
function mVoteClick () { | |
voted = !voted; | |
mVoteUpdate(); | |
var req = new XMLHttpRequest(); | |
req.addEventListener("load", function () { | |
if (this.status === 204) return; //204. No Content. | |
var res = JSON.parse(this.response); | |
if (res.error) { | |
alert("Failed with error:\n\n" + res.error); | |
} | |
}); | |
//Send our vote | |
req.open("POST", `https://www.khanacademy.org/api/internal/discussions/voteentity?entity_key=${attr.key}&vote_type=${voted ? 1 : 0}`); | |
req.setRequestHeader("X-KA-FKey", cookie); | |
req.send(); | |
} | |
function mVoteUpdate () { | |
voteText.innerText = voted ? "Voted Up!" : "Vote Up"; | |
voteCount.innerText = orgVotes + (voted ? 1 : 0); | |
if (voted) { | |
voteButton.classList.replace(unvotedClass, votedClass); | |
}else { | |
voteButton.classList.replace(votedClass, unvotedClass); | |
} | |
} | |
newWrap.addEventListener("click", mVoteClick); | |
oldWrap.parentNode.insertBefore(newWrap, oldWrap); | |
oldWrap.parentNode.removeChild(oldWrap); | |
mVoteUpdate(); | |
})("©Matthias"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment