From a49e51978cd0d2db7099e5abe95c0583829ad908 Mon Sep 17 00:00:00 2001 From: Chuang Zhu Date: Sun, 20 Oct 2024 10:55:39 +0800 Subject: [PATCH] Add support for browser policy --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ js/background.js | 22 +++++++++++++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4adac23..8c3e679 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,42 @@ toolbarbutton#toggle-button--redirectoreinaregilssoncom-redirector[image*="disab ``` If you don't know what the `userChrome.css` file is, or how to edit it, please look it up on a Firefox forum instead of asking about it in this repository. Thanks! + +## Policy Support + +Redirector supports configuring through browser policies. + +* Firefox: https://mozilla.github.io/policy-templates/#3rdparty +* Chromium: https://www.chromium.org/administrators/configuring-policy-for-extensions/ + +```json +{ + "policies": { + "3rdparty": { + "Extensions": { + "redirector@einaregilsson.com": { + "redirects": [ + { + "description": "Example redirect, try going to http://example.com/anywordhere", + "exampleUrl": "http://example.com/some-word-that-matches-wildcard", + "exampleResult": "https://google.com/search?q=some-word-that-matches-wildcard", + "error": null, + "includePattern": "http://example.com/*", + "excludePattern": "", + "patternDesc": "Any word after example.com leads to google search for that word.", + "redirectUrl": "https://google.com/search?q=$1", + "patternType": "W", + "processMatches": "noProcessing", + "disabled": true, + "grouped": false, + "appliesTo": [ + "main_frame" + ] + } + ] + } + } + } + } +} +``` diff --git a/js/background.js b/js/background.js index 4bdba26..66e6dfc 100644 --- a/js/background.js +++ b/js/background.js @@ -192,7 +192,7 @@ function setUpRedirectListener() { chrome.webRequest.onBeforeRequest.removeListener(checkRedirects); //Unsubscribe first, in case there are changes... chrome.webNavigation.onHistoryStateUpdated.removeListener(checkHistoryStateRedirects); - storageArea.get({redirects:[]}, function(obj) { + getRedirects(function(obj) { var redirects = obj.redirects; if (redirects.length == 0) { log('No redirects defined, not setting up listener'); @@ -256,6 +256,20 @@ function updateIcon() { }); } +function getRedirects(callback) { + if (chrome.storage.managed instanceof Object) { + chrome.storage.managed.get('redirects', function (obj) { + if (obj) { + callback(obj); + } else { + storageArea.get({redirects: []}, callback); + } + }); + } else { + storageArea.get({redirects: []}, callback); + } +} + //Firefox doesn't allow the "content script" which is actually privileged //to access the objects it gets from chrome.storage directly, so we @@ -265,9 +279,7 @@ chrome.runtime.onMessage.addListener( log('Received background message: ' + JSON.stringify(request)); if (request.type == 'get-redirects') { log('Getting redirects from storage'); - storageArea.get({ - redirects: [] - }, function (obj) { + getRedirects(function (obj) { log('Got redirects from storage: ' + JSON.stringify(obj)); sendResponse(obj); log('Sent redirects to content page'); @@ -469,4 +481,4 @@ function handleStartup(){ //This doesn't work yet in Chrome, but we'll put it here anyway, in case it starts working... let darkModeMql = window.matchMedia('(prefers-color-scheme: dark)'); darkModeMql.onchange = updateIcon; -} \ No newline at end of file +}