share-to-mastodon/js/background.js

156 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-12-18 19:47:46 +01:00
// Initialize welcome message and context menu entry on extension load
chrome.runtime.onInstalled.addListener(function (details) {
// Show welcome message
if (details.reason === 'install' || details.reason === 'update') {
// Set message
const notification = {
type: 'basic',
iconUrl: chrome.runtime.getURL('img/icon_x128.png'),
title: 'Share to Mastodon ' + chrome.runtime.getManifest().version + ' installed!',
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 = [
2022-12-18 19:47:46 +01:00
{
title: 'Open Settings'
},
{
title: 'Join Discord'
}
]
2022-12-18 19:47:46 +01:00
}
// Send notification
chrome.notifications.create(notification, () => {
// Handle notification click
chrome.notifications.onClicked.addListener(function () {
2022-12-19 06:16:04 +01:00
chrome.tabs.create({ url: 'https://corbin.io/introducing-share-to-mastodon/' })
2022-12-18 19:47:46 +01:00
})
// Handle notification button clicks
if (!window.navigator.userAgent.includes('Firefox')) {
chrome.notifications.onButtonClicked.addListener(function (_, buttonIndex) {
if (buttonIndex === 0) {
chrome.runtime.openOptionsPage()
} else if (buttonIndex === 1) {
// Open Discord
chrome.tabs.create({ url: 'https://discord.com/invite/59wfy5cNHw' })
}
})
}
2022-12-18 19:47:46 +01:00
})
}
2023-02-20 08:27:17 +01:00
// Initialize context menu
createContextMenu()
// Migrate data if needed
migrateOldData()
2022-12-18 19:47:46 +01:00
})
2023-02-20 07:32:58 +01:00
// Function for migrating data from version 1.0
async function migrateOldData() {
// Chrome/Edge version saved a single server in a "userServer" string in chrome.storage.sync
// Firefox version saved it in the same "userServer" string, but in chrome.local.sync
if (window.navigator.userAgent.includes('Firefox')) {
var data = await chrome.storage.local.get()
} else {
var data = await chrome.storage.sync.get()
}
2023-02-20 07:32:58 +01:00
if (data.userServer) {
var oldServer = data.userServer
2023-02-20 07:37:38 +01:00
console.log('Migrating server selection ' + oldServer + ' to new format...')
2023-02-20 07:32:58 +01:00
// Move data
2023-02-20 08:27:17 +01:00
await chrome.storage.sync.set({ serverList: [oldServer] })
2023-02-20 07:32:58 +01:00
// Delete old data
await chrome.storage.local.clear()
2023-02-20 07:32:58 +01:00
await chrome.storage.sync.remove('userServer')
console.log('Migration complete!')
}
}
2023-02-17 23:12:59 +01:00
// Function for creating context menu entries
async function createContextMenu() {
// Remove existing entries if they exist
await chrome.contextMenus.removeAll()
// Create a menu entry for each saved server
var data = await chrome.storage.sync.get()
if ((!data.serverList) || (data.serverList.length === 0)) {
// Create generic menu item because no servers are set yet
chrome.contextMenus.create({
id: "generic",
title: 'Share to Mastodon',
contexts: ['selection', 'link', 'page']
})
} else {
// Create menu item for each saved server
for (server in data.serverList) {
var serverUrl = data.serverList[server]
chrome.contextMenus.create({
id: serverUrl,
title: 'Share to ' + serverUrl,
contexts: ['selection', 'link', 'page']
})
}
// Add seperator and link to settings
chrome.contextMenus.create({
id: 'none',
type: 'separator',
contexts: ['selection', 'link', 'page']
})
chrome.contextMenus.create({
id: 'edit-servers',
title: 'Edit server list',
contexts: ['selection', 'link', 'page']
})
}
}
// Function for handling context menu clicks
chrome.contextMenus.onClicked.addListener(async function (info, tab) {
// Open settings page if requested
if (info.menuItemId === 'edit-servers') {
chrome.runtime.openOptionsPage()
return false
}
// Set link and description
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'
}
// Open popup
createPopup(info.menuItemId, shareLink, shareText, tab)
})
// Reload context menu options on storage change (e.g. when added or removed on settings page)
2023-02-20 08:27:17 +01:00
chrome.storage.onChanged.addListener(function () {
2023-02-17 23:12:59 +01:00
createContextMenu()
})
2022-12-18 19:47:46 +01:00
// Function for creating share popup
2023-02-17 23:12:59 +01:00
function createPopup(serverUrl, shareLink, shareText, tab) {
var popupPage = chrome.runtime.getURL('share.html') + '?server=' + serverUrl + '&link=' + encodeURIComponent(shareLink) + '&text=' + encodeURIComponent(shareText)
2022-12-18 19:47:46 +01:00
var popupWidth = 500
var popupHeight = 500
2022-12-19 05:52:52 +01:00
var y = Math.round((tab.height / 2) - (popupHeight / 2))
var x = Math.round((tab.width / 2) - (popupWidth / 2))
console.log('Popup dimensions:', popupWidth, popupHeight, y, x)
2022-12-18 19:47:46 +01:00
chrome.windows.create({
url: popupPage,
width: popupWidth,
height: popupHeight,
left: x,
top: y,
type: 'popup'
})
}
2022-12-18 22:51:11 +01:00
// Function for action button
chrome.action.onClicked.addListener(async function (tab) {
2023-02-17 23:12:59 +01:00
createPopup('generic', tab.url, tab.title, tab)
2023-02-20 08:27:17 +01:00
})