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 support for browser policy #399

Open
wants to merge 1 commit 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
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;
}
}