From caf6e4d69f2c603626842eae0ef30f612a8525d6 Mon Sep 17 00:00:00 2001 From: Clozent <45975432+Clozent@users.noreply.github.com> Date: Fri, 11 Aug 2023 17:18:37 +0300 Subject: [PATCH 1/2] Add option to skip redirection Add the option to skip redirection of a link from the context menu. --- js/background.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/js/background.js b/js/background.js index 4bdba26..f7502c2 100644 --- a/js/background.js +++ b/js/background.js @@ -32,6 +32,9 @@ var justRedirected = { }; var redirectThreshold = 3; +//Whether or not to skip redirection for the next request on a specific URL. +var skipRedirect = []; + function setIcon(image) { var data = { path: {} @@ -50,10 +53,54 @@ function setIcon(image) { }); } +//Create the context menu options for opening a link without redirecting. +chrome.contextMenus.create( + { + id: "open-without-redirect", + title: "Open without redirecting", + contexts: ["link"], + }, +); + +chrome.contextMenus.create( + { + id: "open-without-redirect-new-tab", + title: "Open in a new tab without redirecting", + contexts: ["link"], + }, +); + +//Listen for clicks on the context menu options. +chrome.contextMenus.onClicked.addListener((info, tab) => { + switch (info.menuItemId) { + case "open-without-redirect": + skipRedirect[info.linkUrl] = true; + chrome.tabs.update({ + url: info.linkUrl, + }); + break; + case "open-without-redirect-new-tab": + skipRedirect[info.linkUrl] = true; + chrome.tabs.create({ + url: info.linkUrl, + }); + break; + } +}); + //This is the actual function that gets called for each request and must //decide whether or not we want to redirect. function checkRedirects(details) { + //Skip redirection and return the original URL if the user chose to open the link + //without redirecting. + if (skipRedirect[details.url]) { + console.log("Skipping redirect!"); + console.log(`(${details.url})`); + skipRedirect[details.url] = false; + return {}; + } + //We only allow GET request to be redirected, don't want to accidentally redirect //sensitive POST parameters if (details.method != 'GET') { diff --git a/manifest.json b/manifest.json index 19b88bb..659ba7e 100644 --- a/manifest.json +++ b/manifest.json @@ -21,7 +21,8 @@ "tabs", "http://*/*", "https://*/*", - "notifications" + "notifications", + "contextMenus" ], "applications": { "gecko": { From 18d9765c10648cf237d84ccc75933f7ef488cd7c Mon Sep 17 00:00:00 2001 From: Clozent <45975432+Clozent@users.noreply.github.com> Date: Fri, 11 Aug 2023 17:20:03 +0300 Subject: [PATCH 2/2] Add partial localization support Add _locales folder and english localization for the two context menu options. --- _locales/en/messages.json | 16 ++++++++++++++++ js/background.js | 4 ++-- manifest.json | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 _locales/en/messages.json diff --git a/_locales/en/messages.json b/_locales/en/messages.json new file mode 100644 index 0000000..acb9d95 --- /dev/null +++ b/_locales/en/messages.json @@ -0,0 +1,16 @@ +{ + "extentionName": { + "message": "Redirector", + "description": "Name of the extention." + }, + + "contextMenuItemNoRedirect": { + "message": "Open without redirecting", + "description": "Context menu option to open a link without attempting to redirect." + }, + + "contextMenuItemNoRedirectNewTab": { + "message": "Open in a new tab without redirecting", + "description": "Context menu option to open a link in a new tab without attempting to redirect." + } +} \ No newline at end of file diff --git a/js/background.js b/js/background.js index f7502c2..00fb521 100644 --- a/js/background.js +++ b/js/background.js @@ -57,7 +57,7 @@ function setIcon(image) { chrome.contextMenus.create( { id: "open-without-redirect", - title: "Open without redirecting", + title: chrome.i18n.getMessage("contextMenuItemNoRedirect"), contexts: ["link"], }, ); @@ -65,7 +65,7 @@ chrome.contextMenus.create( chrome.contextMenus.create( { id: "open-without-redirect-new-tab", - title: "Open in a new tab without redirecting", + title: chrome.i18n.getMessage("contextMenuItemNoRedirectNewTab"), contexts: ["link"], }, ); diff --git a/manifest.json b/manifest.json index 659ba7e..9070357 100644 --- a/manifest.json +++ b/manifest.json @@ -40,6 +40,7 @@ "page": "popup.html", "chrome_style": true }, + "default_locale": "en", "browser_action": { "default_icon": { "16": "images/icon-light-theme-16.png",