Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Last active March 10, 2020 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0 to your computer and use it in GitHub Desktop.
Save CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0 to your computer and use it in GitHub Desktop.
add share button for dartpad, for UserScript/ tampermonkey, you just add token for input
// ==UserScript==
// @name Shard in dartpad
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Caijinglong
// @match https://dartpad.cn/*
// @match https://dartpad.dev/*
// @grant none
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js
// ==/UserScript==
// gist of the userscript: https://gist.github.com/CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0
let tokenKey = "githubToken";
function refreshToken() {
window.githubToken = localStorage.getItem(tokenKey);
}
function tokenIsNull() {
let githubToken = window.githubToken;
console.log(`githubToken = ${githubToken}`);
return (
typeof githubToken == "undefined" ||
githubToken == null ||
githubToken === ""
);
}
function saveToken(token) {
localStorage.setItem(tokenKey, token);
}
function inputToken() {
let token = prompt(
"Enter your github access token for gist, which is stored in your localStorage and used to create gist. It will not be uploaded anywhere.",
window.githubToken
);
console.log(`input token = ${token}`);
if (!(typeof token == "undefined" || token === null || token === "")) {
window.githubToken = token;
saveToken(window.githubToken);
}
}
function createButton(id, text) {
let div = document.createElement("div");
let button = document.createElement("button");
button.innerText = text;
button.id = id;
button.className = "mdc-button mdc-ripple-upgraded";
div.append(button);
return div;
}
(function () {
"use strict";
refreshToken();
if (tokenIsNull()) {
inputToken();
}
let header = $("header")[0];
header.append(createButton("custom_share", "Share to gist of github by userscript"));
header.append(createButton("edit_token", "Edit github token"));
header.append(createButton("test_share_api", "Test usability"));
$("#custom_share").click(function () {
// shared to
if (tokenIsNull()) {
inputToken();
}
if (tokenIsNull()) {
alert("Cannot share, please check your token");
return;
}
share();
});
$("#edit_token").click(function () {
inputToken();
});
$("#test_share_api").click(function () {
testShareToken();
});
})();
function share() {
let button = $("#custom_share");
button.attr("disabled", "true");
let data = JSON.parse(localStorage.getItem("gist"));
data.description = prompt("title for share");
if (!data.description) {
data.description = "share by dartpad";
}
data.files["README.md"] = {
content: "Create by https://gist.github.com/CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0",
};
$.ajax({
url: "https://api.github.com/gists",
method: "POST",
headers: {
Authorization: `token ${window.githubToken}`
},
contentType: "application/json",
data: JSON.stringify(data),
dataType: "json",
success: function (event) {
let id = event.id;
let shareUrl = `https://dartpad.cn/${id}`;
copyText(shareUrl);
alert(`share url: https://dartpad.cn/${id}\ngist url: ${event.html_url}`);
button.removeAttr("disabled");
},
error: function (err) {
if (err.status !== 200) {
alert("The token is incorrect, it cannot access your gist.");
} else {
alert("The token is correct and can be shared normally.");
}
button.removeAttr("disabled");
}
});
}
function testShareToken() {
$.ajax({
url: "https://api.github.com/gists/starred",
method: "GET",
headers: {
Authorization: `token ${window.githubToken}`
},
success: function (event) {
alert("The token is correct and can be shared normally.");
},
error: function (err) {
if (err.status !== 200) {
alert("The token is incorrect, it cannot access your gist.");
} else {
alert("The token is correct and can be shared normally");
}
}
});
}
function copyText(text) {
let copyText = document.createElement("textarea");
copyText.innerHTML =
'<textarea readonly id="copy_text" style="position:absolute;left:-9999px"></textarea>';
document.body.append(copyText);
copyText.innerHTML = text;
copyText.readOnly = false;
copyText.select();
copyText.setSelectionRange(0, copyText.value.length);
document.execCommand("copy");
copyText.readOnly = true;
copyText.remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment