Implemented changeInstance shortcut in a different way #112

This commit is contained in:
ManeraKai 2022-03-15 21:55:03 +03:00
parent 3950812dd9
commit 5582bde76b
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
3 changed files with 21 additions and 28 deletions

View File

@ -45,5 +45,13 @@
"encoding": "UTF-8",
"is_default": false
}
},
"commands": {
"changeInstance": {
"suggested_key": {
"default": "Alt+Shift+L"
},
"description": "Change Instance"
}
}
}

View File

@ -167,14 +167,6 @@ browser.tabs.onUpdated.addListener(
if (instagramHelper.isBibliogram(url)) instagramHelper.initBibliogramCookies(url);
// if (changeInfo.url && youtubeHelper.isPipedorInvidious(url, 'main_frame', 'pipedMaterial')) youtubeHelper.initPipedMaterialLocalStorage(tabId);
if (changeWholeInstance(url))
browser.tabs.executeScript(
tabId,
{
file: "/pages/background/shortcuts.js",
runAt: "document_start"
}
);
});
function changeWholeInstance(url) {
@ -203,12 +195,18 @@ function changeWholeInstance(url) {
return newUrl;
}
browser.runtime.onMessage.addListener(
message => {
if (message.function === 'changeInstance') {
const url = new URL(message.url);
let newUrl = changeWholeInstance(url);
if (newUrl) browser.tabs.update({ url: newUrl });
}
browser.commands.onCommand.addListener(
command => {
if (command === 'changeInstance')
chrome.tabs.query(
{ active: true, currentWindow: true },
tabs => {
let url;
try { url = new URL(tabs[0].url); }
catch (_) { return }
let newUrl = changeWholeInstance(url);
if (newUrl) browser.tabs.update({ url: newUrl });
}
);
}
)

View File

@ -1,13 +0,0 @@
"use strict";
window.browser = window.browser || window.chrome;
document.addEventListener('keydown',
e => {
if (e.code === 'KeyL' && e.shiftKey && e.altKey)
browser.runtime.sendMessage({
function: "changeInstance",
url: window.location.href,
});
}
)