2023-02-21 00:18:31 +01:00
|
|
|
const isFirefox = chrome.runtime.getURL('').startsWith('moz-extension://')
|
|
|
|
|
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') {
|
2023-07-11 06:21:55 +02:00
|
|
|
chrome.tabs.create({ 'url': chrome.runtime.getURL('welcome.html') });
|
|
|
|
};
|
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() {
|
2023-02-20 23:12:50 +01:00
|
|
|
// Chrome/Edge version saved a single server in a "userServer" string in chrome.storage.sync
|
2023-02-20 23:55:55 +01:00
|
|
|
// Firefox version saved it in the same "userServer" string, but in chrome.local.sync
|
2023-02-21 00:18:31 +01:00
|
|
|
if (isFirefox) {
|
2023-02-20 23:12:50 +01:00
|
|
|
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
|
2023-02-20 23:12:50 +01:00
|
|
|
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']
|
|
|
|
})
|
|
|
|
}
|
2023-02-24 18:41:21 +01:00
|
|
|
// Add seperator and link to settings, but only if there's more than one server saved
|
|
|
|
if (data.serverList.length > 1) {
|
|
|
|
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']
|
|
|
|
})
|
|
|
|
}
|
2023-02-17 23:12:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
2023-02-20 23:55:55 +01:00
|
|
|
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
|
|
|
})
|