Giter Site home page Giter Site logo

blk's Introduction

blk

A simple bash script that makes it a breeze to block distracting websites.

Installation

brew tap mhgbrg/homebrew-tap
brew install blk

To block websites blk adds a line to the computer's hosts file that redirects the website to localhost. Depending on the permissions on your hosts file, you might need to run the script with sudo. If you don't want to use sudo, you can change the permissions of the hosts file with chmod 646 /etc/hosts so that your user gets permission to write (this makes using the script so much more convenient).

Also make sure you have enabled the atrun daemon if you want to use automatic blocking/unblocking. See requirements for more info.

Usage

To block a few websites:

blk block news.ycombinator.com www.facebook.com www.youtube.com

Since this is the most common action, the block keyword can be omitted:

blk news.ycombinator.com www.facebook.com www.youtube.com

To unblock:

blk unblock news.ycombinator.com www.facebook.com www.youtube.com

You can also block/unblock for a limited time period:

blk news.ycombinator.com for 2 hours

Or until a specific time:

blk news.ycombinator.com until 21:00

To view all blocked and temporary unblocked websites, use the list command:

blk list

To unblock all currently blocked websites, use the unblock command with the all parameter:

blk unblock all

You can also block all websites in a predefined file by using the block command with the all parameter:

blk block all

Or simply blk all (since block can be omitted). This command reads sites from the file ~/.blk-list. If you want to use a different file, set the environment variable BLK_LIST to its path. For example, putting export BLK_LIST=~/.distracting-websites.txt in your .bashrc or .zshrc will make the above command use ~/.distracting-websites.txt:

You can also feed sites to blk through stdin, for example:

echo "news.ycombinator.com" | blk
blk < ~/.distracting-websites.txt

Requirements

To use blk you need to have bash installed. I would recommend updating to the latest version if you haven't.

To use the for and until functionality you need to have the at command installed and working. On macOS at is installed per default, but the atrun daemon is not running. To fix this, run launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist and restart your computer (see man atrun for more info).

License

MIT

blk's People

Contributors

mhgbrg avatar

Stargazers

 avatar Tony Le avatar Filip Hallqvist avatar  avatar  avatar Fredrik Kindström avatar

Watchers

 avatar

Forkers

hjorthjort

blk's Issues

Show if websites are blocked for or until a time in blk list

Right now there is no indication whether a website is blocked for 2 seconds or eternally. This should be shown when using blk list.

To do this the information could be saved after '# blk' in the hosts file. For example '# blk until 17.30'.

Another interesting thing could be to also show sites that are unblocked for or until a certain time. Information about this is currently not saved in the hosts file, but it could be stored in commented out lines. Another solution is to save a file with state in the user's home folder with this information.

Write tests

The functionality of blk is beginning to get too big to manually test each time some change is made. I don't know if writing tests for bash scripts are considered good practice (or if it's just an indication of that your script is beginning to get too large...), but it is something that could be looked into.

Remove need for using sudo

It is a drag to always have to use sudo for blocking or unblocking websites. Can sudo be incorporated in the script in some way?

Maybe it is as easy as adding a section in the readme about changing the permission of the hosts file.

Use a linter to check for code errors

I don't know anything about bash, so there are probably a whole lot of errors in the code. Using a linter would teach me more about bash and help me fix some of these errors.

Leverage more of at's power

at can take arguments such as now + 2 minutes. Right now the script only parses one parameter after the keyword until. Maybe it could be worth it to parse any extra parameters to leverage the complete power of at.

Prettier output

Right now the script outputs one line for each website blocked or unblocked. It would probably be nicer to just output one line for all websites, or something similar.

Make it easier to block websites

It is a hassle to have to supply the -s flag each time a site is blocked. Could this be make easier? Maybe the first argument always have to be the site that is blocked or unblocked.

Using `until` for several websites at once fails

When using until for several websites at once, only one of the websites are blocked/unblocked at the specified time. This is because the at-jobs both try to edit the hosts-file at the same time, which results in only one of the succeeding.

Redirect to other sites served by HTTPS

Right now when redirecting a site to another site served by HTTPS the certificate for the site redirected to is not valid, since the url in the browser doesn't match the url for the certificate. True redirects are not supported by the hosts file, so the easiest solution to this problem would probably be to redirect to a local web server that can redirect to the correct site.

This web server would have to run on the default port, since you can't specify which port to redirect to in the hosts file.

Update readme

The way the script is used has been changed and the examples in the readme needs to be updated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.