commit 54765b91388864f81b56227783680c2a613d5cbe Author: smpl Date: Thu May 5 05:13:58 2022 +0200 ENERGIZE! diff --git a/_locales/en/messages.json b/_locales/en/messages.json new file mode 100644 index 0000000..c63b6ed --- /dev/null +++ b/_locales/en/messages.json @@ -0,0 +1,22 @@ +{ + "menuItemShareLink": { + "message": "Share Link", + "description": "Share the right-clicked link." + }, + "menuItemSharePage": { + "message": "Share Page", + "description": "Share current page." + }, + "menuItemShareImage": { + "message": "Share Link to Image", + "description": "Share link to the right-clicked image." + }, + "menuItemShareFrame": { + "message": "Share Link to IFrame", + "description": "Share link to the right-clicked iframe." + }, + "menuItemShareSelection": { + "message": "Share Selected Text", + "description": "Share the selected text." + } +} diff --git a/icons/mastodon-share-dark.svg b/icons/mastodon-share-dark.svg new file mode 100644 index 0000000..8cb4a2a --- /dev/null +++ b/icons/mastodon-share-dark.svg @@ -0,0 +1,38 @@ + + + + + + + + + + + + diff --git a/icons/mastodon-share-light.svg b/icons/mastodon-share-light.svg new file mode 100644 index 0000000..ea32d7c --- /dev/null +++ b/icons/mastodon-share-light.svg @@ -0,0 +1,38 @@ + + + + + + + + + + + + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..f4f9d30 --- /dev/null +++ b/manifest.json @@ -0,0 +1,38 @@ +{ + "manifest_version": 2, + "name": "Mastodon Share", + "version": "0.1", + "default_locale": "en", + "description": "Share links on Mastodon", + + "browser_specific_settings": { + "gecko": { + "id": "mastodonshare@slamkode.ml", + "strict_min_version": "57.0" + } + }, + + "background": { + "scripts": ["mastodonshare.js"] + }, + + "permissions": ["contextMenus", "storage"], + + "browser_action": { + "default_icon": "icons/mastodon-share-light.svg", + "default_title": "Share on Mastodon", + "default_popup": "popup.html", + "theme_icons": [ + { + "light": "icons/mastodon-share-light.svg", + "dark": "icons/mastodon-share-dark.svg", + "size": 16 + }, + { + "light": "icons/mastodon-share-light.svg", + "dark": "icons/mastodon-share-dark.svg", + "size": 32 + } + ] + } +} diff --git a/mastodonshare.js b/mastodonshare.js new file mode 100644 index 0000000..88f0bca --- /dev/null +++ b/mastodonshare.js @@ -0,0 +1,110 @@ +var data = { + account: "", + sharebase: "" +}; + +function SetupAccount(result) +{ + if(!result.account) { + console.log("No account in storage."); + return; + } + data.account = result.account; + + if(data.account.charAt(0) == '@') { + data.account = data.account.substring(1); + } + + var uriparts = data.account.split('@'); + + if(uriparts.length < 2) + return; + data.sharebase = 'https://' + uriparts[1] + '/share?text='; +} + +browser.storage.local.get("account").then(SetupAccount); + +browser.contextMenus.create({ + id: "share-link", + title: browser.i18n.getMessage("menuItemShareLink"), + contexts: ["link"] +}); + +browser.contextMenus.create({ + id: "share-page", + title: browser.i18n.getMessage("menuItemSharePage"), + contexts: ["page"] +}); + +browser.contextMenus.create({ + id: "share-image", + title: browser.i18n.getMessage("menuItemShareImage"), + contexts: ["image"] +}); + +browser.contextMenus.create({ + id: "share-frame", + title: browser.i18n.getMessage("menuItemShareFrame"), + contexts: ["frame"] +}); + +browser.contextMenus.create({ + id: "share-selection", + title: browser.i18n.getMessage("menuItemShareSelection"), + contexts: ["selection"] +}); + +browser.contextMenus.onClicked.addListener((info, tab) => +{ + var text; + + if(data.account === "") { + console.log("Error: No account."); + return; + } + + switch (info.menuItemId) { + case "share-link": { + text = info.linkUrl; + break; + } + case "share-page": { + text = info.pageUrl + break; + } + case "share-image": { + text = info.srcUrl; + break; + } + case "share-frame": { + text = info.frameUrl; + break; + } + case "share-selection": { + text = info.selectionText; + break; + } + default: { + return; + } + } + + browser.tabs.update({ + "url": data.sharebase + encodeURIComponent(text) + }); +}); + +browser.runtime.onMessage.addListener( async (msg) => { + if(msg.key === "getAccount") { + browser.storage.local.get("account").then(SetupAccount); + return data.account; + } + if(msg.key === "setAccount") { + browser.storage.local.set({ + account: msg.value + }).then( result => { + data.account = msg.value; + }); + return; + } +}); diff --git a/popup.css b/popup.css new file mode 100644 index 0000000..9d9d498 --- /dev/null +++ b/popup.css @@ -0,0 +1,63 @@ +@media (prefers-color-scheme: dark) { + body { + background: #282c37 !important; + color: #9baec8 !important; + } + .mastodon-widget-title { + color: #fff !important; + } + .mastodon-widget-input { + background: #131419 !important; + color: #fff !important; + } +} +body { + background: #d9e1e8; + color: #282c37; +} +.mastodon-widget-container { + font-family: "Roboto",Roboto,sans-serif; + margin: 2px auto; + padding: 20px; + font-weight: 400; + font-size: 13px; + line-height: 18px; +} +.mastodon-widget-input { + border: 0; + border-radius: 4px; + margin: 8px 0; + padding: 10px; + width: 100%; + box-sizing: border-box; + font-family: inherit; + font-weight: 500; + font-size: 18px; + background: #f9fafb; + color: #000; +} +.mastodon-widget-title { + text-align: center; + font-size: 18px; + font-weight: 500; + margin: 0; + color: #282c37; +} +.mastodon-widget-button { + display: block; + width: 100%; + border: 0; + border-radius: 4px; + margin: 8px 0; + padding: 10px; + background: #2b90d9; + color: #fff; + font-family: inherit; + font-weight: 500; + font-size: 18px; + line-height: inherit; + text-align: center; + text-transform: uppercase; + box-sizing: border-box; + cursor: pointer; +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..40f360f --- /dev/null +++ b/popup.html @@ -0,0 +1,11 @@ + + + + +
+

Mastodon Account

+ + +
+ + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..3ea3b08 --- /dev/null +++ b/popup.js @@ -0,0 +1,25 @@ +function saveOptions(e) +{ + browser.runtime.sendMessage( + { + "key": "setAccount", + "value": document.querySelector(".mastodon-widget-input").value + } + ).then( + response => { + window.close(); + } + ); +} + +document.addEventListener("DOMContentLoaded", function() { + document.removeEventListener("DOMContentLoaded", this); + + browser.runtime.sendMessage({"key": "getAccount"}).then( + response => { + document.querySelector(".mastodon-widget-input").value = response; + } + ); + + document.querySelector(".mastodon-widget-button").addEventListener("click", saveOptions); +});