Skip to content

Instantly share code, notes, and snippets.

@valencik
Forked from anonymous/README.md
Created September 25, 2015 22:21
Show Gist options
  • Save valencik/a37bcc9e9ac1beeddbf5 to your computer and use it in GitHub Desktop.
Save valencik/a37bcc9e9ac1beeddbf5 to your computer and use it in GitHub Desktop.
Quick Gist: The self replicating chrome extension
  • Download this gist as a zip and extract
  • Open chrome://extensions
  • Enable Developer mode
  • Click Load unpacked extension... and select the extracted folder
  • 💵 Profit 💵

Created by the SMU CS Society

{
"manifest_version": 2,
"name": "Quick Gist",
"description": "Get the gist out.",
"version": "1.0.0",
"browser_action": {
"default_title": "Create Gist",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
]
}
<html><head><style>
* {
font-size: 1.25em;
}
body {
padding: 1em;
width: 400px;
max-width: 800px;
}
input, textarea {
width: 100%;
}
textarea {
margin-bottom: 1em;
}
#replicate {
float: right;
}
#smucsm {
color: #aaa;
font-size: 0.75em;
position: absolute;
bottom: 0;
right: 5px;
text-decoration: none;
}
#smucsm:hover,
#smucsm:active,
#smucsm:visited, {
color: #aaa;
}
</style>
</head><body><div>
<h1>Create a Gist</h1>
<form id="coolform">
<input name="description" type="text" placeholder="Description">
<textarea name="content" placeholder="...Content..." rows="10"></textarea>
<button type="submit">Save</button>
<button id="replicate">Self Replicate</button>
</form>
<a id="smucsm" target="_blank" href="http://smucsm.github.io">Created by the SMU CS Society</a>
</div>
<script src="popup.js"></script></body></html>
var coolform = document.getElementById("coolform");
var replicate = document.getElementById("replicate");
coolform.onsubmit = function(e){
e.preventDefault();
var target = e.target
var description = target.elements.namedItem("description").value;
var content = target.elements.namedItem("content").value;
var filename = new Date().toISOString().slice(0,10) + ".md";
var githubRequest = toGitHubObject(description, [{name:filename, content:content}]);
genericRequest("POST", 'https://api.github.com/gists', JSON.stringify(githubRequest));
};
replicate.onclick = function (){
var manifestFile,
popupjs,
popuphtml = document.documentElement.outerHTML;
// -------
genericRequest("GET", "manifest.json", null, function(request){
if (request.target.status >= 200 && request.target.status < 400) {
var response = request.target.responseText;
manifestFile = response;
// -------
genericRequest("GET", "popup.js", null, function(request){
if (request.target.status >= 200 && request.target.status < 400) {
var response = request.target.responseText;
popupjs = response;
// -------
var githubRequest = toGitHubObject("Quick Gist: The self replicating chrome extension", [
{name:"README.md", content:"- Download this gist as a zip and extract\n- Open `chrome://extensions`\n- Enable `Developer mode`\n- Click `Load unpacked extension...` and select the extracted folder\n- :dollar: Profit :dollar:\n\nCreated by the [SMU CS Society](http://smucsm.github.io)"},
{name:"manifest.json", content: manifestFile},
{name:"popup.js", content: popupjs},
{name:"popup.html", content: popuphtml}
]);
genericRequest("POST", 'https://api.github.com/gists', JSON.stringify(githubRequest));
} else {
console.log("Error", request);
}
});
} else {
console.log("Error", request);
}
});
}
function genericRequest(method, url, data, success, error){
if (method == null || url == null){
console.log('missing request, method or url');
return;
}
if (method != "GET" && method != "POST"){
console.log('invalid request type');
return;
}
var request = new XMLHttpRequest();
request.open(method, url, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
if (success){
request.onload = success;
}
else {
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var response = JSON.parse(request.responseText);
console.log("Success", response.html_url);
// Open new tab with response.html_url
chrome.tabs.create({
url: response.html_url
});
} else {
console.log("Error", request);
}
};
}
if (error){
request.onerror = error;
}
else {
request.onerror = function(){
console.log("Error", request);
}
}
request.send(data);
}
// files = [{name: "filename", content: "file content"}]
function toGitHubObject(description, files){
var githubRequest = {};
githubRequest.description = description;
githubRequest.public = true;
githubRequest.files = {};
//unpack files to github's files object
files.forEach(function(file){
githubRequest.files[file.name] = {
content: file.content
};
});
return githubRequest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment