Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.
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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.bat
*.txt
*.xpi
dist
node_modules
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
124 changes: 124 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict'
/* globals browser */

const {
formatISODateTime,
getState,
setState,
syncState
} = require('./lib/state')
const moment = require('moment')
const {
quotaTick,
currentStatus,
affectsUrl,
nextNonBlockingMoment
} = require('./lib/logic')

function getActiveUrl () {
return browser.windows.getLastFocused({populate: true, windowTypes: ['normal']}).then(activeWindow => {
if (activeWindow.focused === true) {
const activeTab = activeWindow.tabs.find(tab => tab.active)
if (activeTab != null) {
return activeTab.url
}
}

return undefined
})
}

function getAllTabs () {
return browser.tabs.query({windowType: 'normal'})
}

function getAllUrls () {
return getAllTabs().then(tabs => tabs.map(tab => tab.url).filter(url => url != null))
}

function blockTab (now, settings, data, tab) {
const blockPageUrl = browser.extension.getURL('block/en-US.html')
let hash = '#'
if (tab.url != null) {
hash += encodeURIComponent(tab.url)
}

hash += '#'
const unblockingMoment = nextNonBlockingMoment(now, settings, data)
if (unblockingMoment != null) {
hash += formatISODateTime(unblockingMoment)
}

hash += '#'
if (settings.name.trim() !== '') {
hash += settings.name
}

browser.tabs.update(tab.id, {url: blockPageUrl + hash})
}

function main () {
getState().then(state => {
syncState(state)

const lastStatuses = {}

setInterval(() => {
const now = moment()
// quotaTick for all block sets
Promise.all(state.blockSetSettings.map(settings => {
const data = state.blockSetData[settings.id]
return quotaTick(getActiveUrl, getAllUrls, now, settings, data)
})).then(() => {
// then: check if their status changed to "blocking", and block all
// tabs with affected urls
for (const settings of state.blockSetSettings) {
const id = settings.id
const data = state.blockSetData[id]
const newStatus = currentStatus(now, settings, data)
if (newStatus === 'blocking' && lastStatuses[id] !== 'blocking') {
getAllTabs().then(tabs => {
Promise.all(tabs.map(tab => {
if (tab.url != null && affectsUrl(tab.url, settings, data)) {
blockTab(now, settings, data, tab)
}
}))
})
}
lastStatuses[id] = newStatus
}
})
}, 1000)

// save block set data every minute
setInterval(
() => setState({blockSetData: state.blockSetData}),
60 * 1000
)

// block a tab if its url changed to something blocked
browser.tabs.onUpdated.addListener((tabId, {url}, tab) => {
if (url == null) {
return
}

const now = moment()
for (const settings of state.blockSetSettings) {
const data = state.blockSetData[settings.id]
if (
currentStatus(now, settings, data) === 'blocking' &&
affectsUrl(url, settings, data)
) {
blockTab(now, settings, data, tab)
}
}
})
})
}

module.exports = {main}

if (typeof browser !== 'undefined') {
// only executed in browser
main()
}
41 changes: 41 additions & 0 deletions block/block.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* LeechBlock CSS style */

body {
background-color: #fff;
color: #2e3436;
font: normal 16px "Open Sans", Arial, sans-serif;
text-align: center;
}

div {
margin-top: 50px;
}

h1 {
font-size: 24px;
font-weight: 300;
}

a {
color: #2ca089;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

.freedom {
font-size: 14px;
}

.support {
color: #777;
font-size: 14px;
font-style: italic;
}

.support a {
color: #777;
text-decoration: underline;
}
66 changes: 66 additions & 0 deletions block/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

function decodeBlockInfo (hash) {
let [, blockedUrl, unblockingDate, blockSetName] = hash.split('#')

if (blockedUrl !== '') {
blockedUrl = decodeURIComponent(blockedUrl)
} else {
blockedUrl = undefined
}

if (unblockingDate !== '') {
unblockingDate = new Date(decodeURIComponent(unblockingDate))
} else {
unblockingDate = undefined
}

if (blockSetName !== '') {
blockSetName = decodeURIComponent(blockSetName)
} else {
blockSetName = undefined
}

return {
blockedUrl,
unblockingDate,
blockSetName
}
}

const {blockedUrl, unblockingDate, blockSetName} =
decodeBlockInfo(window.location.hash)

const blockedUrlLink = document.getElementById('blocked-url-link')
if (blockedUrl != null) {
blockedUrlLink.appendChild(document.createTextNode(blockedUrl))
blockedUrlLink.setAttribute('href', blockedUrl)
} else {
blockedUrlLink.style.display = 'none'
}

if (unblockingDate != null) {
const now = new Date()
if (unblockingDate < now && blockedUrl != null) {
window.location.href = blockedUrl
}
let formatted
if (unblockingDate.toDateString() === now.toDateString()) {
formatted = unblockingDate.toLocaleTimeString()
} else {
formatted = unblockingDate.toLocaleString()
}

document.getElementById('never-unblocked').style.display = 'none'
document.getElementById('unblock-time')
.appendChild(document.createTextNode(formatted))
} else {
document.getElementById('sometime-unblocked').style.display = 'none'
}

if (blockSetName != null) {
document.getElementById('block-set-name')
.appendChild(document.createTextNode(blockSetName))
} else {
document.getElementById('block-set-info').style.display = 'none'
}
34 changes: 34 additions & 0 deletions block/en-US.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>Site Blocked</title>
<meta charset="utf-8">
<link rel="shortcut icon" href="../assets/leechblock16.ico">
<link rel="stylesheet" href="block.css" type="text/css">
</head>

<body>
<!-- logo -->
<div>
<img src="../assets/leechblock_logo.png" alt="LeechBlock logo" title="LeechBlock" />
</div>

<!-- warning message -->
<div>
<h1>The page you're attempting to access has been blocked by <a href="http://www.proginosko.com/leechblock/">LeechBlock</a></h2>
</div>

<!-- blocked site info -->
<div>
<a id="blocked-url-link"></a> <span id="block-set-info">(<span id="block-set-name"></span>)</span>
<p id="never-unblocked">
The page will <strong>never</strong> be unblocked.
</p>
<p id="sometime-unblocked">
The page will be unblocked at <strong id="unblock-time"></strong>.
</p>
</div>

<script src="block.js"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

PATH="$PATH:$(npm bin)"

mkdir -p dist/firefox

cp -r assets dist/firefox
cp -r manifest.json dist/firefox/manifest.json

# TODO: disable -d in release
browserify -d background.js > dist/firefox/background.js

cp -r settings dist/firefox
# TODO: disable -d in release
browserify -d settings/settings.js > dist/firefox/settings/settings.js

cp -r block dist/firefox
11 changes: 0 additions & 11 deletions chrome.manifest

This file was deleted.

30 changes: 0 additions & 30 deletions chrome/content/accesscode.js

This file was deleted.

44 changes: 0 additions & 44 deletions chrome/content/accesscode.xul

This file was deleted.

Loading