Fix notifications and keyboard shortcut on Firefox

This commit is contained in:
Corbin Davenport 2023-02-20 17:55:55 -05:00
parent 4c414f729a
commit 64eb4ea632
3 changed files with 30 additions and 14 deletions

View File

@ -7,15 +7,18 @@ chrome.runtime.onInstalled.addListener(function (details) {
type: 'basic', type: 'basic',
iconUrl: chrome.runtime.getURL('img/icon_x128.png'), iconUrl: chrome.runtime.getURL('img/icon_x128.png'),
title: 'Share to Mastodon ' + chrome.runtime.getManifest().version + ' installed!', title: 'Share to Mastodon ' + chrome.runtime.getManifest().version + ' installed!',
buttons: [ message: "Click here to see what's new in this version."
}
// Firefox doesn't support buttons in notifications
if (!window.navigator.userAgent.includes('Firefox')) {
notification.buttons = [
{ {
title: 'Open Settings' title: 'Open Settings'
}, },
{ {
title: 'Join Discord' title: 'Join Discord'
} }
], ]
message: "Click here to see what's new in this version."
} }
// Send notification // Send notification
chrome.notifications.create(notification, () => { chrome.notifications.create(notification, () => {
@ -24,14 +27,16 @@ chrome.runtime.onInstalled.addListener(function (details) {
chrome.tabs.create({ url: 'https://corbin.io/introducing-share-to-mastodon/' }) chrome.tabs.create({ url: 'https://corbin.io/introducing-share-to-mastodon/' })
}) })
// Handle notification button clicks // Handle notification button clicks
chrome.notifications.onButtonClicked.addListener(function (_, buttonIndex) { if (!window.navigator.userAgent.includes('Firefox')) {
if (buttonIndex === 0) { chrome.notifications.onButtonClicked.addListener(function (_, buttonIndex) {
chrome.runtime.openOptionsPage() if (buttonIndex === 0) {
} else if (buttonIndex === 1) { chrome.runtime.openOptionsPage()
// Open Discord } else if (buttonIndex === 1) {
chrome.tabs.create({ url: 'https://discord.com/invite/59wfy5cNHw' }) // Open Discord
} chrome.tabs.create({ url: 'https://discord.com/invite/59wfy5cNHw' })
}) }
})
}
}) })
} }
// Initialize context menu // Initialize context menu
@ -43,7 +48,7 @@ chrome.runtime.onInstalled.addListener(function (details) {
// Function for migrating data from version 1.0 // Function for migrating data from version 1.0
async function migrateOldData() { async function migrateOldData() {
// Chrome/Edge version saved a single server in a "userServer" string in chrome.storage.sync // Chrome/Edge version saved a single server in a "userServer" string in chrome.storage.sync
// Firefox version saved it in "userServer" string but in chrome.local.sync // Firefox version saved it in the same "userServer" string, but in chrome.local.sync
if (window.navigator.userAgent.includes('Firefox')) { if (window.navigator.userAgent.includes('Firefox')) {
var data = await chrome.storage.local.get() var data = await chrome.storage.local.get()
} else { } else {
@ -134,7 +139,7 @@ function createPopup(serverUrl, shareLink, shareText, tab) {
var popupHeight = 500 var popupHeight = 500
var y = Math.round((tab.height / 2) - (popupHeight / 2)) var y = Math.round((tab.height / 2) - (popupHeight / 2))
var x = Math.round((tab.width / 2) - (popupWidth / 2)) var x = Math.round((tab.width / 2) - (popupWidth / 2))
console.log(popupWidth, popupHeight, y, x) console.log('Popup dimensions:', popupWidth, popupHeight, y, x)
chrome.windows.create({ chrome.windows.create({
url: popupPage, url: popupPage,
width: popupWidth, width: popupWidth,

View File

@ -51,7 +51,13 @@ document.querySelector('#server-remove-btn').addEventListener('click', function
// Open keyboard shortcut // Open keyboard shortcut
document.querySelector('#mastodon-keyboard-shortcut').addEventListener('click', function () { document.querySelector('#mastodon-keyboard-shortcut').addEventListener('click', function () {
chrome.tabs.create({ url: 'chrome://extensions/shortcuts#:~:text=Share%20to%20Mastodon' }) // Firefox cannot automatically navigate to the keyboard shortcut configuration page
if (window.navigator.userAgent.includes('Firefox')) {
const bsCollapse = new bootstrap.Collapse('#firefoxShortcutCollapse').show()
} else {
chrome.tabs.create({ url: 'chrome://extensions/shortcuts#:~:text=Share%20to%20Mastodon' })
}
}) })
loadSettings() loadSettings()

View File

@ -36,6 +36,11 @@
<div class="mb-3"> <div class="mb-3">
<label class="form-label">Other settings</label> <label class="form-label">Other settings</label>
<button type="button" class="btn btn-sm btn-primary w-100" id="mastodon-keyboard-shortcut">Change keyboard shortcut</button> <button type="button" class="btn btn-sm btn-primary w-100" id="mastodon-keyboard-shortcut">Change keyboard shortcut</button>
<div class="collapse mt-1" id="firefoxShortcutCollapse">
<div class="card card-body">
Open about:addons in a new tab, click the settings gear button, then select "Manage extension shortcuts."
</div>
</div>
</div> </div>
<hr> <hr>
<p>If you find Share to Mastodon useful, please donate to support continued development. It would mean a lot!</p> <p>If you find Share to Mastodon useful, please donate to support continued development. It would mean a lot!</p>