Skip to content

Instantly share code, notes, and snippets.

@wernersa
Created July 11, 2023 15:30
Show Gist options
  • Save wernersa/0691ee96f2fd9a2ddc1e5cd777d204f0 to your computer and use it in GitHub Desktop.
Save wernersa/0691ee96f2fd9a2ddc1e5cd777d204f0 to your computer and use it in GitHub Desktop.
Userscript: Upload chrome local storage to glot.io (pastebin alternative)
// ==UserScript==
// @name Export token
// @version 0.1
// @description Retrieve token from local storage and upload to glot.io (pastebin alternative)
// @author Werner
// @match https://example.com
// @run-at document-end
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
console.log("Token: " + localStorage.token); //The name of the localstorage variable is "token"
function urlEncodeObject(obj, prefix = '') {
const encodedPairs = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
const encodedKey = encodeURIComponent(key);
if (typeof value === 'object' && !Array.isArray(value)) {
const nestedPrefix = prefix ? `${prefix}[${encodedKey}]` : encodedKey;
const encodedNested = urlEncodeObject(value, nestedPrefix);
encodedPairs.push(encodedNested);
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
const arrayPrefix = `${prefix}[${encodedKey}]`;
const encodedArrayItem = urlEncodeObject({ [i]: value[i] }, arrayPrefix);
encodedPairs.push(encodedArrayItem);
}
} else {
const encodedValue = encodeURIComponent(value);
const encodedPair = `${prefix ? `${prefix}[${encodedKey}]` : encodedKey}=${encodedValue}`;
encodedPairs.push(encodedPair);
}
}
}
return encodedPairs.join('&');
}
const data = {
'language': 'plaintext',
'title': 'This is the title of the document',
'public': false,
'files': [
{
'name': 'main.txt',
'content': localStorage.token
}
]
};
GM_xmlhttpRequest({
method: "PUT",
url: 'https://glot.io/api/snippets/XXXXXXXXXX',
data: JSON.stringify(data),
headers: {
'Authorization': 'Token XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'Content-type': 'application/json'
},
onload: function(response) {
console.log(response.status, response.responseText);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment