Create bookmarklet

This commit is contained in:
Corbin Davenport 2022-12-23 00:38:50 -05:00 committed by GitHub
parent da7730096f
commit 9a61097951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 0 deletions

32
BOOKMARKLET.md Normal file
View File

@ -0,0 +1,32 @@
# Share to Mastodon Bookmarklet
This bookmarklet replicates most of the functionality of the Share to Mastodon extension, except that opening the bookmark is the only way to activate it. The extension has additional options, like an item in the browser's context menu and a keyboard shortcut.
### The bookmark
Open the bookmark manager in your web browser (`chrome://bookmarks/` in Chrome, Bookmarks > Edit Bookmarks in Safari, etc.) and create a new bookmark with the below text string as the URL. **Near the start of the URL, replace "mastodon.social" with your Mastodon server, if it's different than mastodon.social.** On Safari, you may have to edit an existing bookmark.
```
javascript:var serverDomain="mastodon.social";var shareLink=window.location.href;var shareText="";var selectedText=window.getSelection().toString();if(selectedText){shareText='"'+selectedText+'"'}else{shareText=document.title}var url="https://"+serverDomain+'/share?text='+encodeURIComponent(shareText+'\n\n'+shareLink);window.open(url,"_blank","popup=true,width=500,height=500,left=100,top=100");
```
Opening the bookmarklet will open the share dialog, so if you have the bookmarks bar always visible, sharing to Mastodon only takes one click.
### Non-minified version
```
var serverDomain = "mastodon.social"
var shareLink = window.location.href
var shareText = ""
var selectedText = window.getSelection().toString()
if (selectedText) {
// Use selected text if available
shareText = '"' + selectedText + '"'
} else {
// Use page title as fallback
shareText = document.title
}
// Open popup
var url = "https://" + serverDomain + '/share?text=' + encodeURIComponent(shareText + '\n\n' + shareLink)
window.open(url, "_blank", "popup=true,width=500,height=500,left=100,top=100")
```