Skip to content

Commit

Permalink
Add support for browser policy
Browse files Browse the repository at this point in the history
  • Loading branch information
chuangzhu committed Oct 20, 2024
1 parent de0f47a commit a49e519
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
"[email protected]": {
"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"
]
}
]
}
}
}
}
}
```
22 changes: 17 additions & 5 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}
}

0 comments on commit a49e519

Please sign in to comment.