fanzine/static/popup/fanzine.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-23 17:25:29 +00:00
AWS.config.update({
region: bucketRegion,
s3: { endpoint: endpoint },
credentials: new AWS.Credentials({
accessKeyId: key,
secretAccessKey: secret,
})
});
2022-03-29 12:45:52 +00:00
/*
* ROUTER/CONTROLLER*/
const state = {}
const router = state => () => {
const index = "home";
if !(window.location.hash) {
window.location.hash = index
}
if (window.location.hash == "home") {
if (!loadCredentials(state)) {
window.location.hash = "configure"
return
}
}
}
const instance = router(state)
window.onhashchange = instance
instance()
2022-03-23 17:25:29 +00:00
2022-03-29 12:45:52 +00:00
/* HELPERS */
const loadCredentials = state => {
if (state.s3) return
s3: new AWS.S3({ apiVersion: "2006-03-01" }),
}
/* EVENTS */
2022-03-23 17:25:29 +00:00
document
.querySelector("#plain-text-send")
.addEventListener("click", () => {
const identifier = new Uint8Array(30);
window.crypto.getRandomValues(identifier);
const ib64 = btoa(String.fromCharCode(...identifier));
const path = `paste/${ib64}.txt`;
2022-03-29 12:45:52 +00:00
const text = document.querySelector("#plain-text-content").value
2022-03-23 17:25:29 +00:00
const params = {
Bucket: bucketName,
Key: path,
ContentType: 'text/plain',
2022-03-29 12:45:52 +00:00
Body: text
2022-03-23 17:25:29 +00:00
};
// Uploading files to the bucket
2022-03-29 12:45:52 +00:00
state.s3.upload(params, function(err, data) {
2022-03-23 17:25:29 +00:00
if (err) {
throw err;
}
const url = `${host}/${path}`;
browser.tabs.create({
url: url,
active: true,
})
document.querySelector("#output").innerHTML = `Succès, votre texte est disponible à cette adresse : ${url}`;
});
})