Skip to content

Commit 889e20d

Browse files
committed
blocks only during specified time
1 parent 6fba34f commit 889e20d

File tree

2 files changed

+114
-23
lines changed

2 files changed

+114
-23
lines changed

src/backgroundScript/applyRules.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
const shouldBlock = (blocked) => {
2+
if (!blocked) return false
3+
4+
const { startDate, endDate } = blocked
5+
if (startDate && new Date(startDate) > Date.now()) return false
6+
if (endDate && new Date(endDate) < Date.now()) return false
7+
return true
8+
}
9+
110
const blockRequest = (union, msg) => {
211
const unionEncoded = encodeURIComponent(union);
312
const msgEncoded = encodeURIComponent(msg);
@@ -20,14 +29,18 @@ const matchesRulePattern = (url) => ({ sites }) => {
2029
}
2130

2231
export default (policy) => ({ url }) => {
23-
return policy.rules
24-
.filter(matchesRulePattern(url))
25-
.reduce((acc, rule) => {
26-
const blocked = rule.actions.find(a => a.action === 'block')
27-
if (blocked) { return blockRequest(policy.name, blocked.message) }
28-
29-
const warn = rule.actions.find(a => a.action === 'warn')
30-
if (warn) { return prepareBanner(url, warn.message) }
31-
return {}
32-
}, {})
32+
const rule = policy.rules
33+
.find(matchesRulePattern(url))
34+
35+
if (!rule) return {}
36+
37+
const blockAction = rule.actions.find(a => a.action === 'block')
38+
if (shouldBlock(blockAction)) {
39+
return blockRequest(policy.name, blockAction.message)
40+
}
41+
42+
const warn = rule.actions.find(a => a.action === 'warn')
43+
if (warn) { return prepareBanner(url, warn.message) }
44+
45+
return {}
3346
}

src/backgroundScript/applyRules.spec.js

+91-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,105 @@
11
import { expect } from 'chai'
22
import applyRules from './applyRules.js'
33

4-
global.chrome = {
5-
runtime: {
6-
getURL: (str) => str
7-
}
8-
}
9-
10-
const policy = {
11-
"name": "pineapple",
12-
"rules": [{
13-
"sites": ["*://www.notarealwebsite.com/*"],
14-
"actions": [ { "action": "block", "message": "not okay" } ]
15-
}]}
4+
global.chrome = { runtime: { getURL: (str) => str } }
5+
const today = new Date(Date.now()).toDateString()
6+
const tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000).toDateString()
7+
const yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000).toDateString()
8+
const dayBefore = new Date(new Date().getTime() - 48 * 60 * 60 * 1000).toDateString()
169

1710
describe('applyRules', () => {
18-
it('cancels blocked url', () => {
11+
it('cancels blocked url with no dates', () => {
12+
const policy = {
13+
"name": "pineapple",
14+
"rules": [{
15+
"sites": ["*://www.notarealwebsite.com/*"],
16+
"actions": [{ "action": "block", "message": "not okay" }]
17+
}]
18+
}
1919
const blockingResponse = applyRules(policy)({ url: 'http://www.notarealwebsite.com' })
2020
const expected = "blocked.html?union=pineapple&msg=not%20okay"
2121
expect(blockingResponse).to.deep.equal({ redirectUrl: expected })
2222
})
2323

24+
it('cancels blocked url with start date but no end date', () => {
25+
const policy = {
26+
"name": "pineapple",
27+
"rules": [{
28+
"sites": ["*://www.today.com/*"],
29+
"actions": [{
30+
"action": "block",
31+
"message": "not okay",
32+
"startDate": today
33+
}]
34+
}]
35+
}
36+
37+
const blockingResponse = applyRules(policy)({ url: 'http://www.today.com' })
38+
const expected = "blocked.html?union=pineapple&msg=not%20okay"
39+
expect(blockingResponse).to.deep.equal({ redirectUrl: expected })
40+
})
41+
42+
it('cancels blocked url with start and end date', () => {
43+
const policy = {
44+
"name": "pineapple",
45+
"rules": [{
46+
"sites": ["*://www.today.com/*"],
47+
"actions": [{
48+
"action": "block",
49+
"message": "not okay",
50+
"startDate": today,
51+
"endDate": tomorrow
52+
}]
53+
}]
54+
}
55+
const blockingResponse = applyRules(policy)({ url: 'http://www.today.com' })
56+
const expected = "blocked.html?union=pineapple&msg=not%20okay"
57+
expect(blockingResponse).to.deep.equal({ redirectUrl: expected })
58+
})
59+
60+
it('does nothing if blocking start date is in the future', () => {
61+
const policy = {
62+
"name": "pineapple",
63+
"rules": [{
64+
"sites": ["*://www.tomorrow.com/*"],
65+
"actions": [{
66+
"action": "block",
67+
"message": "not okay",
68+
"startDate": tomorrow
69+
}]
70+
}]
71+
}
72+
const blockingResponse = applyRules(policy)({ url: 'http://www.tomorrow.com' })
73+
const expected = "blocked.html?union=pineapple&msg=not%20okay"
74+
expect(blockingResponse).to.deep.equal({})
75+
})
76+
77+
it('does nothing if blocking end date is passed', () => {
78+
const policy = {
79+
"name": "pineapple",
80+
"rules": [{
81+
"sites": ["*://www.yesterday.com/*"],
82+
"actions": [{
83+
"action": "block",
84+
"message": "not okay",
85+
"startDate": dayBefore,
86+
"endDate": yesterday
87+
}]
88+
}]
89+
}
90+
const blockingResponse = applyRules(policy)({ url: 'http://www.tomorrow.com' })
91+
const expected = "blocked.html?union=pineapple&msg=not%20okay"
92+
expect(blockingResponse).to.deep.equal({})
93+
})
94+
2495
it('does nothing if url not listed', () => {
96+
const policy = {
97+
"name": "pineapple",
98+
"rules": [{
99+
"sites": ["*://www.notarealwebsite.com/*"],
100+
"actions": [{ "action": "block", "message": "not okay" }]
101+
}]
102+
}
25103
const blockingResponse = applyRules(policy)({ url: 'http://abc' })
26104

27105
expect(blockingResponse).to.deep.equal({})

0 commit comments

Comments
 (0)