#!/usr/bin/env perl # # This program toggles the hosts_css adblocking on the Maemo platform. # It is assumed that the hosts and css files have already been setup. # To use this file just (as root) run #./hosts_css_toggler.pl # or using rootsh: #rootsh ./hosts_css_toggler.pl # It should automagically toggle the changes on and off # Just restart the browser for changes to take effect # # It assumes that you've just appended the full text to your existing /etc/hosts file # It just moves the userContent.css to userContent.css.bkp, so if you have # custom additions to it your results will be undesired # # Author: # Kyle Dickerson - kyle.dickerson@gmail.com # Free to use, modify, whatever # You can ask me for help, but I don't guarantee any useful support # # P.S. This is basically the first Perl I've written, so go ahead and laugh, but it does work $hosts_filename = "/etc/hosts"; $css_filename = "/home/user/.mozilla/microb/chrome/userContent.css"; print "Toggling Hosts/CSS Based Ad-blocking...\n"; # First deal with the hosts file open(HOSTS, $hosts_filename); @hosts_lines = ; close(HOSTS); if (substr($hosts_lines[0], 0, 19) ne '# host_css_toggled=') { unshift(@hosts_lines, "# host_css_toggled=on\n"); print "Installing Toggler\n"; } if ($hosts_lines[0] eq "# host_css_toggled=on\n") { $hosts_lines[0] = "# host_css_toggled=off\n"; $turn_on = 0; print "\tToggling OFF: $hosts_filename... "; } else { $hosts_lines[0] = "# host_css_toggled=on\n"; $turn_on = 1; print "\tToggling ON: $hosts_filename... "; } $start_toggling = 0; foreach $host_line (@hosts_lines) { if (!$start_toggling && substr($host_line, 0, 14) eq "# Mike Skallas") { $start_toggling = 1; } if ($start_toggling) { if (!$turn_on && substr($host_line, 0, 9) eq '127.0.0.1') { $host_line = '#hct# ' . $host_line; } elsif ($turn_on && substr($host_line, 0, 6) eq '#hct# ') { substr($host_line, 0, 6, ''); } } } print "DONE!\n"; open(HOSTS, ">$hosts_filename"); print HOSTS @hosts_lines; close(HOSTS); # Now Deal with the userContent.css file if ($turn_on) { print "\tToggling ON: $css_filename... "; if (-e $css_filename . ".bkp") { rename($css_filename . ".bkp", $css_filename); print "DONE!\n"; } else { print "Error: Could not find: $css_filename.bkp"; } } else { print "\tToggling OFF: $css_filename... "; if (-e $css_filename) { rename($css_filename, $css_filename . ".bkp"); print "DONE!\n"; } else { print "Error: Could not find: $css_filename"; } }