Created
July 11, 2023 15:30
-
-
Save wernersa/0691ee96f2fd9a2ddc1e5cd777d204f0 to your computer and use it in GitHub Desktop.
Userscript: Upload chrome local storage to glot.io (pastebin alternative)
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
// ==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