client for blocking

This commit is contained in:
cupcakearmy 2020-09-20 22:24:57 +02:00
parent c078f3150e
commit 77564060e2
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
12 changed files with 81 additions and 22 deletions

View File

@ -2,11 +2,10 @@
## Current ## Current
- d3 graph - Max time for website -> block.
## Backlog ## Backlog
- Max time for website -> block
- Dark mode support - Dark mode support
- Options - Options
- Dashboard - Dashboard

View File

@ -29,7 +29,7 @@
"content_scripts": [ "content_scripts": [
{ {
"matches": ["<all_urls>"], "matches": ["<all_urls>"],
"js": [] "js": ["./src/client/index.js"]
} }
], ],
"web_accessible_resources": ["./icons/stopwatch.svg", "./icons/stopwatch-inv.svg"] "web_accessible_resources": ["./icons/stopwatch.svg", "./icons/stopwatch-inv.svg"]

View File

@ -2,9 +2,9 @@
"name": "ora", "name": "ora",
"private": true, "private": true,
"scripts": { "scripts": {
"clean": "rm -rf dist .cache",
"dev": "parcel watch --no-hmr manifest.json src/dashboard/index.html", "dev": "parcel watch --no-hmr manifest.json src/dashboard/index.html",
"build": "parcel build --no-content-hash --no-source-maps manifest.json src/dashboard/index.html", "build": "parcel build --no-content-hash --no-source-maps manifest.json src/dashboard/index.html"
"launch": "web-ext run -s dist"
}, },
"browserslist": [ "browserslist": [
"last 2 chrome versions", "last 2 chrome versions",

View File

@ -1,7 +1,8 @@
import browser from 'webextension-polyfill' import browser from 'webextension-polyfill'
import { dashboard } from '../shared/utils' import { dashboard } from '../shared/utils'
import { insertLog, normalizeTimestamp } from '../shared/db' import { insertLog, normalizeTimestamp, Limits } from '../shared/db'
import { getUsageForHost, percentagesToBool } from '../shared/lib'
browser.browserAction.onClicked.addListener(() => browser.tabs.create({ url: dashboard, active: true })) browser.browserAction.onClicked.addListener(() => browser.tabs.create({ url: dashboard, active: true }))
@ -37,3 +38,8 @@ async function getAllTabs() {
setInterval(() => { setInterval(() => {
getAllTabs() getAllTabs()
}, frequency) }, frequency)
browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
// await Limits.
return getUsageForHost(message).then((percentages) => percentagesToBool(percentages))
})

44
src/client/index.js Normal file
View File

@ -0,0 +1,44 @@
import browser from 'webextension-polyfill'
let wrapper
function init() {
wrapper = window.document.createElement('div')
Object.assign(wrapper.style, {
display: 'none',
position: 'fixed',
top: '0',
left: '0',
width: '100vw',
height: '100vh',
backgroundColor: '#ffffff',
zIndex: '999999999',
})
wrapper.classList.add('ora--wrapper')
wrapper.classList.add('hidden')
const inner = window.document.createElement('div')
Object.assign(inner.style, {
fontFamily: `-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif`,
margin: '3em auto',
width: '100%',
maxWidth: '20em',
})
inner.innerHTML = `
<h1>Overtime </h1>
<p>You have no time left on this website 🥺</p>
`
wrapper.appendChild(inner)
window.document.body.appendChild(wrapper)
}
async function check() {
if (window.document.hidden) return
console.log('Checking')
const isBlocked = await browser.runtime.sendMessage(window.location.host)
wrapper.style.display = isBlocked ? 'initial' : 'none'
}
init()
setInterval(check, 5000)
check()

View File

@ -1,17 +1,10 @@
<script> <script>
import Router, { link } from 'svelte-spa-router' import Router, { link } from 'svelte-spa-router'
import dj from 'dayjs'
import RelativeTime from 'dayjs/plugin/relativeTime'
import Duration from 'dayjs/plugin/duration'
import Dev from './components/Dev.svelte' import Dev from './components/Dev.svelte'
import Dashboard from './pages/Dashboard.svelte' import Dashboard from './pages/Dashboard.svelte'
import Limits from './pages/Limits.svelte' import Limits from './pages/Limits.svelte'
dj.extend(Duration)
dj.extend(RelativeTime)
const routes = { const routes = {
'/': Dashboard, '/': Dashboard,

View File

@ -1,10 +1,11 @@
<script> <script>
import { onMount } from 'svelte'
import dj from 'dayjs' import dj from 'dayjs'
import DateInput from './DateInput.svelte' import DateInput from './DateInput.svelte'
export let start export let start = new Date()
export let end export let end = new Date()
function set(interval, amount = 1) { function set(interval, amount = 1) {
return () => { return () => {
@ -19,7 +20,7 @@
} }
// Init // Init
set('week')() onMount(() => set('day', 0)())
</script> </script>
<style> <style>

View File

@ -1,7 +1,7 @@
<script> <script>
import dj from 'dayjs' import dj from 'dayjs'
import { getUsageForRules } from '../lib.js' import { getUsageForRules } from '../../shared/lib.js'
export let host = '' export let host = ''
export let rules = [] export let rules = []

View File

@ -4,7 +4,7 @@
import Chart from '../components/Chart.svelte' import Chart from '../components/Chart.svelte'
import RangeChooser from '../components/RangeChooser.svelte' import RangeChooser from '../components/RangeChooser.svelte'
import { data, countInGroup } from '../lib' import { data, countInGroup } from '../../shared/lib'
let top = 15 let top = 15
let full = 50 let full = 50

View File

@ -5,7 +5,7 @@
import Rules from '../components/Rules.svelte' import Rules from '../components/Rules.svelte'
import { Limits } from '../../shared/db.js' import { Limits } from '../../shared/db.js'
import { longPress } from '../lib' import { longPress } from '../../shared/lib'
let limits = null let limits = null
let limit = null let limit = null

View File

@ -1,5 +1,10 @@
import NeDB from 'nedb-promises' import NeDB from 'nedb-promises'
import day from 'dayjs' import dj from 'dayjs'
import RelativeTime from 'dayjs/plugin/relativeTime'
import Duration from 'dayjs/plugin/duration'
dj.extend(Duration)
dj.extend(RelativeTime)
export const Logs = NeDB.create({ export const Logs = NeDB.create({
filename: 'logs.db', filename: 'logs.db',
@ -17,7 +22,7 @@ export function clear() {
export function normalizeTimestamp(timestamp) { export function normalizeTimestamp(timestamp) {
// Normalize every dato to 15 minutes // Normalize every dato to 15 minutes
const t = day(timestamp) const t = dj(timestamp)
const min = t.minute() const min = t.minute()
return t return t
.millisecond(0) .millisecond(0)

View File

@ -1,7 +1,7 @@
import { groupBy, orderBy, sum } from 'lodash' import { groupBy, orderBy, sum } from 'lodash'
import dj from 'dayjs' import dj from 'dayjs'
import { Logs } from '../shared/db' import { Limits, Logs } from './db.js'
export async function data({ start, end }) { export async function data({ start, end }) {
const logs = await getLogsBetweenDates({ start, end }) const logs = await getLogsBetweenDates({ start, end })
@ -51,3 +51,14 @@ export function getUsageForRules(host, rules) {
return (consumed / limitAsSeconds) * 100 return (consumed / limitAsSeconds) * 100
}) })
} }
export async function getUsageForHost(host) {
const limit = await Limits.findOne({ host })
return await Promise.all(getUsageForRules(host, limit.rules))
}
export function percentagesToBool(percentages) {
const blocked = percentages.map((p) => p >= 100).includes(true)
console.log(percentages, blocked)
return blocked
}