summaryrefslogtreecommitdiff
path: root/extensions/perdomainsettings
blob: ce39fcc8260cd1d3f7ca1c18ba5e9aa6f2cb4e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
//  Copyright (c) 2012-2013 Stefan Bolte <portix@gmx.net>
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with this program; if not, write to the Free Software Foundation, Inc.,
//  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//


/*
 * Per domain settings extension
 *
 * This extensions can be used for per-domain-settings. Valid settings are
 * the properties of WebKitWebSettings but in camelcase, see 
 * http://webkitgtk.org/reference/webkitgtk/unstable/WebKitWebSettings.html
 * for details. 
 *
 * To use this extension load it with a userscript in 
 * $HOME/.config/dwb/userscripts/, e.g. 
 *
 * ------------------------------------------------------------------------------
 * |#!javascript                                                                | 
 * |                                                                            | 
 * |extensions.load("perdomainsettings");                                       | 
 * ------------------------------------------------------------------------------
 *
 * The config can consist of four objects, 
 *
 * domains:   Settings applied based on the second level domain
 *
 * hosts:     Settings applied based on the hostname
 *
 * uris:      Settings applied based on the uri
 *
 * defaults:  Default settings, for each setting in domains, hosts and uris a
 *              default-value should be specified
 *
 * Example extensionrc: 
 *
 * ------------------------------------------------------------------------------
 * |return {                                                                    |
 * |   foo : { ... },                                                           |
 * |                                                                            |
 * |   perdomainsettings : {                                                    |
 * |     domains : {                                                            |
 * |        "example.com" : {                                                   |
 * |             "enablePlugins"  : true                                        |
 * |        },                                                                  |
 * |        "example.uk.com" :  {                                               |
 * |             "enablePlugins"  : true,                                       |
 * |             "enableScripts"  : false                                       |
 * |        }                                                                   |
 * |     },                                                                     |
 * |     hosts : {                                                              |
 * |        "www.example1.com" :  {                                             |
 * |             "autoLoadImages" : true                                        |
 * |        }                                                                   |
 * |     },                                                                     |
 * |     uris : {                                                               |
 * |        "http://www.example2.com/login.php" :  {                            |
 * |             "autoLoadImages" : false                                       |
 * |        }                                                                   |
 * |     },                                                                     |
 * |     defaults : {                                                           |
 * |        "enablePlugins"   : false,                                          |
 * |        "autoLoadImages"  : false,                                          |
 * |        "enableScripts"   :  true                                           |
 * |     }                                                                      |
 * |   },                                                                       |
 * |                                                                            |
 * |   bar : { ... }                                                            |
 * |}                                                                           |
 * ------------------------------------------------------------------------------
 *
 * Example using extensions.load: 
 *
 * ------------------------------------------------------------------------------
 * |extensions.load("perdomainsettings", {                                      |
 * |  domains : { "example.com" : { "enablePlugins" : true }  },                |
 * |  defaults : { "enablePlugins" : false }                                    |
 * |});                                                                         |
 * ------------------------------------------------------------------------------
 *
 * */ 
/*<INFO
Change webkit-settings automatically on domain or url basis
INFO>*/

var domains = null;
var hosts = null;
var uris = null;
var exports = {}; 
var defaults = {};


var defaultConfig = {
//<DEFAULT_CONFIG
// Only webkit builtin settings can be set, for a list of settings see 
// http://webkitgtk.org/reference/webkitgtk/unstable/WebKitWebSettings.html
// All settings can also be used in camelcase, otherwise they must be quoted.
// 
// The special domain suffix .tld matches all top level domains, e.g. 
// example.tld matches example.com, example.co.uk, example.it ... 
//
// Settings based on uri will override host based settings and host based
// settings will override domain based settings. Settings for domains/hosts/uris
// with without tld suffix will override settings for
// domains/hosts/uris with tld suffix respectively, e.g. 
//      "example.com" : { enableScripts : true }, 
//      "example.tld" : { enableScripts : false } 
// will enable scripts on example.com but not on example.co.uk, example.it, ... 


// Settings applied based on the second level domain
domains : {
//      "example.com" : { "auto-load-images" : false }, 
//      "google.tld" : { enableScripts : false, autoLoadImages : false }, 
},

//Settings applied based on the hostname
hosts : {
//    "www.example.com" : { autoLoadImages : true } 
},

// Settings applied based on the uri
uris : {
//  "http://www.example.com/foo/" : { autoLoadImages : true } }, 
},

//>DEFAULT_CONFIG
};

function apply(o, settings) 
{
    var key;
    var defaults = true;
    var websettings = o.settings;
    for (key in settings) 
    {
        if (!o.set[key]) 
        {
            websettings[key] = settings[key];
            o.set[key] = true;
        }
        else 
            defaults = false;
    }
    return defaults;
}

function onNavigation(wv, frame, request, action) 
{
    if (wv.mainFrame != frame)
        return false;

    var value;

    var o = wv.getPrivate("pds", script);
    if (! o)
    {
        o = { webview : wv, defaults : false, settings : wv.settings };
        wv.setPrivate("pds", o, script);
    }
    o.set = {};
    var uri = request.uri;
    var host = request.message.uri.host;
    var domain = util.domainFromHost(host);
    var rTld = new RegExp(domain.substring(domain.indexOf(".") + 1) + "$");
    var domainTld = domain.replace(rTld, "tld");
    var hostTld = host.replace(rTld, "tld");
    var uriTld = uri.replace(new RegExp("(https?://)" + RegExp.escape(host)), "$1" + hostTld);
    if ((value = domains[domain]))
    {
        apply(o, value);
        o.defaults = false;
    }
    if ((value = domains[domainTld]))
    {
        apply(o, value);
        o.defaults = false;
    }
    if ((value = hosts[host])) 
    {
        apply(o, value);
        o.defaults = false;
    }
    if ((value = hosts[hostTld]))
    {
        apply(o, value);
        o.defaults = false;
    }
    if ((value = uris[uri])) 
    {
        apply(o, value);
        o.defaults = false;
    }
    if ((value = uris[uriTld])) 
    {
        apply(o, value);
        o.defaults = false;
    }
    if (o.defaults === false && apply(o, defaults)) 
        o.defaults = true;
    return false;
}
function getDefaultValue(object) 
{
    var key, s, o;
    for(key in object)
    {
        o = object[key];
        for (s in o)
        {
            if (!defaults[s])
                defaults[s] = global.settings[s];
        }
    }
}

return {
    exports : exports, 
    init : function (config) 
    {
        if (!config) 
            return false;

        exports.domains = domains = config.domains || defaultConfig.domains;
        domains && getDefaultValue(domains);

        exports.hosts = hosts = config.hosts || defaultConfig.hosts;
        hosts && getDefaultValue(hosts);

        exports.uris = uris = config.uris || defaultConfig.uris;
        uris && getDefaultValue(uris);

        signals.connect("navigation", onNavigation.debug(script));
        return true;
    },
    end : function () 
    {
        signals.disconnect(onNavigation);
    }
};

// vim: set ft=javascript: