Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip redirection and some localization support #360

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -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."
}
}
47 changes: 47 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Expand All @@ -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: chrome.i18n.getMessage("contextMenuItemNoRedirect"),
contexts: ["link"],
},
);

chrome.contextMenus.create(
{
id: "open-without-redirect-new-tab",
title: chrome.i18n.getMessage("contextMenuItemNoRedirectNewTab"),
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') {
Expand Down
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"tabs",
"http://*/*",
"https://*/*",
"notifications"
"notifications",
"contextMenus"
],
"applications": {
"gecko": {
Expand All @@ -39,6 +40,7 @@
"page": "popup.html",
"chrome_style": true
},
"default_locale": "en",
"browser_action": {
"default_icon": {
"16": "images/icon-light-theme-16.png",
Expand Down