Skip to content

Instantly share code, notes, and snippets.

@tkit1994
Last active July 8, 2022 10:23
Show Gist options
  • Select an option

  • Save tkit1994/cace80dfd3487af8ff6e43061b54e5db to your computer and use it in GitHub Desktop.

Select an option

Save tkit1994/cace80dfd3487af8ff6e43061b54e5db to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name LeetcodeCopy
// @version 1.0.0
// @description Add a copy button in leetcode.cn's editor to copy code in vim mode
// @author HaoCheng
// @namespace com.github.tkit1994
// @license MIT
// @homepage https://github.com/tkit1994/leetcode-copy
// @updateURL https://gist.github.com/tkit1994/cace80dfd3487af8ff6e43061b54e5db/raw/459fe2fc3da6ada3f68cdc2747d458b2e24f63f4/leetcode-copy.user.js
// @downloadURL https://gist.github.com/tkit1994/cace80dfd3487af8ff6e43061b54e5db/raw/459fe2fc3da6ada3f68cdc2747d458b2e24f63f4/leetcode-copy.user.js
// @supportURL https://github.com/tkit1994/leetcode-copy
// @match https://leetcode.cn/problems/*
// @run-at document-body
// @grant GM_setClipboard
// @require https://unpkg.com/[email protected]
// ==/UserScript==
(function(axios) {
"use strict";
;
function _interopDefaultLegacy(e) {
return e && typeof e === "object" && "default" in e ? e : { "default": e };
}
var axios__default = /* @__PURE__ */ _interopDefaultLegacy(axios);
async function getActiveSessionID() {
let data = JSON.stringify({
operationName: "userStatusGlobal",
variables: {},
query: "query userStatusGlobal {\n userStatus {\n isSignedIn\n isAdmin\n isStaff\n isSuperuser\n isTranslator\n isVerified\n isPhoneVerified\n isWechatVerified\n checkedInToday\n username\n realName\n userSlug\n groups\n avatar\n optedIn\n requestRegion\n region\n socketToken\n activeSessionId\n permissions\n completedFeatureGuides\n useTranslation\n accountStatus {\n isFrozen\n inactiveAfter\n __typename\n }\n __typename\n }\n}\n"
});
let resp = await axios__default["default"].post("https://leetcode.cn/graphql/noj-go", data, {
withCredentials: true,
headers: {
"content-type": "application/json"
}
});
return resp.data["data"]["userStatus"]["activeSessionId"];
}
async function getQuestionID() {
let div = document.querySelector("div.active__2qli");
let id = div.getAttribute("data-question-id");
return id;
}
async function ButtonClickAction() {
let sessionID = await getActiveSessionID();
let questionID = await getQuestionID();
let lang_key = `${sessionID}_${questionID}_lang`;
let lang = JSON.parse(localStorage.getItem(lang_key)) || "cpp";
let code_key = `${sessionID}_${questionID}_${lang}_code`;
let code = JSON.parse(localStorage.getItem(code_key));
navigator.clipboard.writeText(code);
}
function addBtn() {
let zNode = document.createElement("div");
zNode.innerHTML = '<button id="myButton" type="button">CopyCode</button>';
zNode.setAttribute("id", "myContainer");
let tmp = document.querySelector("div.second-section-container__2cAh");
tmp.appendChild(zNode);
document.getElementById("myButton").addEventListener("click", ButtonClickAction, false);
}
window.addEventListener("load", function() {
addBtn();
}, false);
})(Axios);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment