Add sharing functionality
This commit is contained in:
parent
1e0255b374
commit
1f9cec2269
2 changed files with 42 additions and 3 deletions
|
@ -12,14 +12,14 @@ html(lang="en")
|
||||||
h1 toot
|
h1 toot
|
||||||
p Share links on any Mastodon instance
|
p Share links on any Mastodon instance
|
||||||
main
|
main
|
||||||
form
|
form#form
|
||||||
section
|
section
|
||||||
label(for="instance") Mastodon instance URL
|
label(for="instance") Mastodon instance URL
|
||||||
input#instance(type="url", name="instance", placeholder="https://")
|
input#instance(type="url", name="instance", placeholder="https://", required)
|
||||||
|
|
||||||
section.remember
|
section.remember
|
||||||
input#remember(type="checkbox", name="remember")
|
input#remember(type="checkbox", name="remember")
|
||||||
label(for="remember") Remember as my default instance
|
label(for="remember") Autofill this instance URL next time
|
||||||
|
|
||||||
section.submit
|
section.submit
|
||||||
input(type="submit", value="Share post")
|
input(type="submit", value="Share post")
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
var remembered = window.localStorage.getItem('mastodon_instance');
|
||||||
|
if (remembered != null) {
|
||||||
|
document.getElementById('instance').value = remembered;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsedQuery = {};
|
||||||
|
|
||||||
|
var paramPairs = window.location.search.substr(1).split('&');
|
||||||
|
var paramPairsLength = paramPairs.length;
|
||||||
|
|
||||||
|
for (var i = 0; i < paramPairsLength; i++) {
|
||||||
|
var paramPair = paramPairs[i].split('=');
|
||||||
|
parsedQuery[paramPair[0]] = paramPair[1];
|
||||||
|
}
|
||||||
|
delete i
|
||||||
|
delete paramPair
|
||||||
|
|
||||||
|
document
|
||||||
|
.getElementById('form')
|
||||||
|
.addEventListener('submit', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var instance = e.target.elements['instance'].value;
|
||||||
|
var remember = e.target.elements['remember'].checked;
|
||||||
|
|
||||||
|
|
||||||
|
if (!(instance.startsWith("https://") || instance.startsWith("http://"))) {
|
||||||
|
instance = "https://" + instance;
|
||||||
|
}
|
||||||
|
if (!instance.endsWith("/")){
|
||||||
|
instance = instance + "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remember) {
|
||||||
|
window.localStorage.setItem('mastodon_instance', instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
var shareUrl = instance + "share?text=" + parsedQuery.text;
|
||||||
|
window.open(shareUrl, '_blank', 'noopener,noreferrer')
|
||||||
|
})
|
Reference in a new issue