From 15b00bde2af031fcc1db7850ec90bb2b323f45d9 Mon Sep 17 00:00:00 2001 From: Corbin Davenport Date: Sun, 12 Feb 2023 02:59:46 -0500 Subject: [PATCH] New share UI now works --- js/background.js | 36 ++++--------------------------- js/share.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ share.html | 37 ++++++++++++++++---------------- 3 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 js/share.js diff --git a/js/background.js b/js/background.js index 0ea6cbe..b22da0b 100644 --- a/js/background.js +++ b/js/background.js @@ -43,9 +43,8 @@ chrome.runtime.onInstalled.addListener(function (details) { }) // Function for creating share popup -function createPopup(serverDomain, shareLink, shareText, tab) { - var test = chrome.runtime.getURL('share.html') - var popupPage = test + '?text=' + encodeURIComponent(shareText + '\n\n' + shareLink) +function createPopup(shareLink, shareText, tab) { + var popupPage = chrome.runtime.getURL('share.html') + '?link=' + encodeURIComponent(shareLink) + '&text=' + encodeURIComponent(shareText) var popupWidth = 500 var popupHeight = 500 var y = Math.round((tab.height / 2) - (popupHeight / 2)) @@ -65,19 +64,6 @@ function createPopup(serverDomain, shareLink, shareText, tab) { chrome.contextMenus.onClicked.addListener(async function (info, tab) { console.log(info, tab) if (info.menuItemId == "share-to-mastodon") { - /* - // Check if there is a saved server - var server = await new Promise(function (resolve) { - chrome.storage.sync.get(function (data) { - resolve(data.userServer) - }) - }) - // Open settings if needed - if (!server) { - chrome.runtime.openOptionsPage() - return false - } - */ // Set link and description var shareLink = '' var shareText = '' @@ -92,25 +78,11 @@ chrome.contextMenus.onClicked.addListener(async function (info, tab) { shareText = 'Type something here' } // Open popup - createPopup('temp', shareLink, shareText, tab) + createPopup(shareLink, shareText, tab) } }) // Function for action button chrome.action.onClicked.addListener(async function (tab) { - // Check if there is a saved server - var server = await new Promise(function (resolve) { - chrome.storage.sync.get(function (data) { - resolve(data.userServer) - }) - }) - // Open settings if needed - if (!server) { - chrome.runtime.openOptionsPage() - return false - } - // Open popup - var shareLink = tab.url - var shareText = tab.title - createPopup(server, shareLink, shareText, tab) + createPopup(tab.url, tab.title, tab) }) \ No newline at end of file diff --git a/js/share.js b/js/share.js new file mode 100644 index 0000000..f7b3879 --- /dev/null +++ b/js/share.js @@ -0,0 +1,55 @@ +/* +var shareLink = '' + var shareText = '' + if (info.linkUrl) { + shareLink = info.linkUrl + shareText = 'Type something here' + } else if (info.selectionText) { + shareLink = info.pageUrl + shareText = '"' + info.selectionText + '"' + } else { + shareLink = info.pageUrl + shareText = 'Type something here' + } + */ + +async function init() { + // Get data from URL and storage + var inputParams = new URL((window.location.href)).searchParams + var shareLink = inputParams.get('link') + var shareText = inputParams.get('text') + var data = await chrome.storage.sync.get() + // Show warning if no servers are saved + if (!data.serverList) { + document.querySelector('#server-warning').classList.remove('d-none') + return false + } + // Create list of servers + var serverListEl = document.querySelector('#server-list') + for (server in data.serverList) { + // Create link list element + var serverUrl = data.serverList[server] + var linkEl = document.createElement('a') + linkEl.classList.add('list-group-item', 'list-group-item', 'list-group-item-action', 'display-6') + linkEl.innerText = serverUrl + linkEl.href = 'https://' + serverUrl + '/share?text=' + encodeURIComponent(shareText + '\n\n' + shareLink) + linkEl.rel = 'preconnect' + // Add server icon to list + var serverImg = document.createElement('img') + serverImg.src = 'https://' + serverUrl + '/favicon.ico' + serverImg.alt = serverUrl + ' icon' + linkEl.prepend(serverImg) + // Inject element + serverListEl.appendChild(linkEl) + } + // Show list + serverListEl.classList.remove('d-none') +} + +// Show loading animation when a link is clicked +window.addEventListener('beforeunload', function() { + document.querySelector('#server-list').classList.add('d-none') + document.querySelector('#server-loading').classList.remove('d-none') +}) + +init() \ No newline at end of file diff --git a/share.html b/share.html index 1120099..6b0a86f 100644 --- a/share.html +++ b/share.html @@ -10,39 +10,40 @@ -
+ +
+ You don't have any servers saved! Why don't you add some? +
+ + + +
+
+ Loading... +
+

You are being redirected to the selected server, please wait.

- +