warren-bank / moz-rewrite

"moz-rewrite" is a Firefox add-on that functions as a light-weight (pseudo) rules-engine for easily modifying HTTP headers in either direction
GNU General Public License v2.0
30 stars 7 forks source link

moz-rewrite

Firefox add-on that functions as a light-weight (pseudo) rules-engine for easily modifying HTTP headers in either direction

Summary

Video Tutorial

2+ hour long screencast that is a detailed walk-through of installation, configuration and usage

Features

Contextual Variables (in scope when functions are called)

Helper Functions (in scope when functions are called)

Data Structure

Simple Examples

[
    /* ****************************************************
     * all requests: add 3 custom headers
     * ****************************************************
     */
    {
        "url" : /^.*$/,
        "headers" : {
            "X-Custom-Sample-Header-01" : "Foo",
            "X-Custom-Sample-Header-02" : "Bar",
            "X-Custom-Sample-Header-03" : "Baz"
        }
    },
    /* ****************************************************
     * secure requests: cancel the 3 custom headers, and stop processing rules
     * ****************************************************
     */
    {
        "url" : /^https/i,
        "headers" : {
            "X-Custom-Sample-Header-01" : false,
            "X-Custom-Sample-Header-02" : false,
            "X-Custom-Sample-Header-03" : false
        },
        "stop": true
    },
    /* ****************************************************
     * all requests: update 1st custom header, cancel 3rd custom header
     * ****************************************************
     */
    {
        "url" : /^.*$/,
        "headers" : {
            "X-Custom-Sample-Header-01" : "Hello",
            "X-Custom-Sample-Header-03" : false
        }
    }
    /* ****************************************************
     * assertion #1: non-secure URL request
     * expected result:
     *     X-Custom-Sample-Header-01: Hello
     *     X-Custom-Sample-Header-02: Bar
     * ****************************************************
     */
]
[
    /* ****************************************************
     * purpose: map an applicable 'content-type' to a finite set of resources
     *          as identified by file extension, when loaded from local hard disk.
     * ****************************************************
     */
    {
        "url" : new RegExp('^file://', 'i'),
        "headers" : function(){
            var $headers = {};
            switch( request.uri.file_ext.toLowerCase() ){
                case 'txt':
                    $headers['content-type'] = 'text/plain';
                    break;
                case 'css':
                    $headers['content-type'] = 'text/css';
                    break;
                case 'js':
                    $headers['content-type'] = 'application/javascript';
                    break;
                case 'json':
                    $headers['content-type'] = 'application/json';
                    break;
            }
            if ( $headers['content-type'] ){
                response.content_type = $headers['content-type'];
            }
            return $headers;
        },
        "stop": true
    }
]

More Complicated Examples

User Preferences

Hidden Preferences

Dialog Windows

Comments / Implementation Notes

Alternate Implementations / Branches

AMO (addons.mozilla.org)

License

GPLv2 Copyright (c) 2014, Warren Bank