Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2011 06:54
Show Gist options
  • Select an option

  • Save anonymous/936184 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/936184 to your computer and use it in GitHub Desktop.
updating Greasemonkey's scriptheader
#!/usr/bin/perl
### The script reads the config.xml looks for the GM scripts, deletes the
### @include and @exclude rules in the GM script and then put the rules from
### config.xml into the appropiate GM script.
###
### Before using the script, please close Firefox and make a backup of the
### gm_script directory (just in case something is going wrong).
###
### Then start the script with the gm_script directory as parameter. Under
### Linux it's usually: /home/USER/.mozilla/firefox/PROFILE/gm_scripts
### Where USER and PROFILE are the corresponding directory names.
use warnings;
use strict;
use File::Copy;
my $gm_dir = shift || die "No GM directory given";
open( my $GM_CONFIG_XML, '<', "$gm_dir/config.xml" ) or die "Couldn't find config in $gm_dir";
my $line;
while ( ! eof $GM_CONFIG_XML ) {
# look for <script> line
next if ($line = <$GM_CONFIG_XML>) !~ /^\s*<script /i;
my ($filename) = $line =~ /filename="(.*?)"/i;
my ($basedir) = $line =~ /basedir="(.*?)"/i;
printf "$basedir/$filename\n";
if ( ! -e "$gm_dir/$basedir/$filename" ) {
print "NOT FOUND - Error in config.xml\n\n";
next;
}
my @includes;
my @excludes;
while ( ! eof $GM_CONFIG_XML ) {
last if ($line = <$GM_CONFIG_XML>) =~ m!</script>!i;
next if $line !~ /<(in|ex)clude>/i;
push @includes, ($line =~ m!<include>\s*(.*?)\s*</include>!i);
push @excludes, ($line =~ m!<exclude>\s*(.*?)\s*</exclude>!i);
}
#
# now change the script
#
open( my $script, '<', "$gm_dir/$basedir/$filename" );
my @write;
while ( my $ll = <$script> ) {
push @write, $ll;
last if $ll =~ m!^\s*//\s*==UserScript==!;
}
# remove include/exclude lines from script - they were only used on install
while ( my $ll = <$script> ) {
last if $ll =~ m!\s*//\s*==/UserScript==!;
next if $ll =~ m!\s*//\s*\@(in|ex)clude\s+(\S+)!;
push @write, $ll;
}
push @write, "// \@include $_\n" for @includes;
push @write, "// \@exclude $_\n" for @excludes;
push @write, "// ==/UserScript==\n";
push @write, $_ while <$script>;
close $script;
# if you want to be perfectly safe uncomment following line,
# but better backup gm_script-tree before using this script
# copy( "$gm_dir/$basedir/$filename", "$gm_dir/$basedir/$filename.bak" );
# open file in clobber-mode so symbolic links are preserved
open( my $NEW_SCRIPT, '+>', "$gm_dir/$basedir/$filename");
print $NEW_SCRIPT @write;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment