aboutsummaryrefslogtreecommitdiffstats
path: root/stylish.js
blob: 255c7f3e1cbc834ed7977e1ec131db822791122d (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
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
    <name>{NAME}</name>
    <description>stylish</description>
    <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
    <version>0.0.1</version>
    <minVersion>2.2pre</minVersion>
    <maxVersion>2.2pre</maxVersion>
    <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/stylish.js</updateURL>
    <license>public domain</license>
    <detail><![CDATA[
]]></detail>
</VimperatorPlugin>;
//}}}

(function(){
let stylishService = stylishOverlay.service;
let control = plugins.stylish = {
    COMMAND_DESCRIPTION: 0,
    COMMAND_FUNCTION: 1,
    COMMAND_COMPLETER: 2,
    commands: {
        edit: {
            description: "Edit style",
            fun: function(args){
                let [cmd, name] = args;
                return control.editStyle(name);
            },
            completer: function(args){
                let title = ["style"];
                let completions = [[s.name,""] for each(s in control.listStyle())];
                return [title, completions];
            },
        },
        new: {
            description: "Write new style",
            fun: function(args){
                let [cmd, rule] = args;
                let code = "@namespace url(http://www.w3.org/1999/xhtml);\n\n";
                if(rule) {
                    code += "@-moz-document " + rule + " {\n}\n";
                }
                return stylishOverlay.addCode(code);
                //return control.newStyle();
            },
            completer: function(args){
                let title = ["rule"];
                let completions = [];
                control.getDomains().forEach(function(x) completions.push(["domain("+x+")", "domain"]));
                if(content&&content.document&&content.document.location){
                    completions.push(["url("+content.document.location+")", "url"]);
                }
                control.getDirectories().forEach(function(x) completions.push(["url-prefix("+x+")", "url-prefix"]));
                return [title, completions];
            },
        },
        dialog: {
            description: "Open stylish dialog",
            fun: function(){
                return stylishOverlay.openManage();
            },
        },
        find: {
            description: "Find styles for this site",
            fun: function(){
                return stylishOverlay.findStyle();
            },
        },
        turnall: {
            description: "Turn all styles",
            fun: function(args){
                let turn = gPrefService.getBoolPref("extensions.stylish.styleRegistrationEnabled");
                let on = false;
                if (args[1]) {
                    if (args[1] == "on")
                      on = true;
                    else if (args[1] != "off")
                        liberator.echoerr("Not a stylish turncommand: " + args[1]);
                } else {
                    on = !turn;
                }
                if (turn != on)
                    gPrefService.setBoolPref("extensions.stylish.styleRegistrationEnabled", on);
                return on;
            },
            completer: function(args)
            [
                ["turnall", "description"],
                [["on","Turn all styles on"], ["off","Turn all styles off"]]
            ],
        },
    },
    listStyle: function()
        [s for each(s in stylishService.list(0, {}))],
    getStyle: function(name)
    {
        let styles = [s for each(s in control.listStyle()) if(s.name==name)];
        if(styles.length == 0) return false
        return styles[0];
    },
    editStyle: function(name)
    {
        if(!name) return control.newStyle();
        let style = control.getStyle(name);
        if(!style) return false;
        return stylishCommon.openEditForStyle(style);
    },
    newStyle: function() {
        var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
        style.mode = style.CALCULATE_META | style.REGISTER_STYLE_ON_CHANGE;
        style.init(null, null, null, null, "", false, null);
        stylishCommon.openEdit(stylishCommon.getWindowName("stylishEdit"), {style: style});
    },
    getDomains: function()
    {
        let domains = [];
        if(content&&content.document&&content.document.domain){
            stylishOverlay.getDomainList(content.document.domain, domains);
        }
        return domains;
    },
    getDirectories: function()
    {
        let uri = buffer.URI;
        let dirs = [];
        while (uri.match(/^(.*?:\/)(.*?)(\/+[^\/]+)\/?$/))
        {
            uri = RegExp.$1 + RegExp.$2 + "/";
            dirs.push(uri);
        }
        return dirs;
    },
    execute: function(args)
    {
        let command = control.commands[args[0]];
        if(command&&command.fun)
            return command.fun(args);
        return false;
    },
    complete: function(args)
    {
        if (args.completeArg == 0){
            let title = ["command", "description"];
            let completions = [[c, control.commands[c].description] for(c in control.commands)];
            return [title, completions];
        }
        let command = control.commands[args[0]];
        if(command&&command.completer)
            return command.completer(args);
        return [];
    }
};
commands.addUserCommand(["stylish"], "stylish command",
    function(args){
        return control.execute(args);
    }, {
        completer: function(context, args){
            [context.title, context.completions] = control.complete(args);
        },
        literal: 1,
    });
})();
// vim: fdm=marker sw=4 ts=4 et:
"o">== Global Variables == maine_coon_targets: フルスクリーン時にの非表示にしたい要素のIDを空白区切りで指定します >|| let g:maine_coon_targets = "sidebar-2 sidebar-2-splitter" ||< maine_coon_default: オプションのデフォルト値を設定します >|| let g:maine_coon_default = "ac" ||< maine_coon_style: メッセージ表示のスタイル指定です >|| let g:maine_coon_style = "border: 1px solid pink; padding: 3px; color: pink; background: black; font: 18px/1 sans-serif;" ||< == Example == === ラージモード(l) 用のマッピング === >|| :noremap <silent> s :set mainecoon!=l<CR> ||< == Thanks == snaka72 (hidechrome part): http://vimperator.g.hatena.ne.jp/snaka72/20090106/1231262955 == メインクーン == http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%82%AF%E3%83%BC%E3%83%B3 ]]></detail> </VimperatorPlugin>; let tagetIDs = (liberator.globalVariables.maine_coon_targets || '').split(/\s+/); let elemStyle = ( liberator.globalVariables.maine_coon_style || libly.$U.toStyleText({ height: '1em', margin: 0, padding: '3px', border: '1px solid #b3b3b3', borderLeft: 0, borderBottom: 0, textAlign: 'left', color: '#000', font: '11px/1 sans-serif', background: '#ebebeb' }) ); (function () { let U = libly.$U; let mainWindow = document.getElementById('main-window'); let messageBox = document.getElementById('liberator-message'); function s2b (s, d) !!((!/^(\d+|false)$/i.test(s)|parseInt(s)|!!d*2)&1<<!s); function hideTargets (hide) { tagetIDs.forEach( function (id) let (elem = document.getElementById(id)) (elem && (elem.collapsed = hide)) ); } function getWindowInfo () { let box = mainWindow.boxObject; let x = screenX < 0 ? 0 : screenX; let y = screenY < 0 ? 0 : screenY; let width = box.width; let height = box.height; let adjustHeight = box.screenY - y; // maybe caption height? let adjustWidth = (box.screenX - x) * 2; return { x: x, y: y, width: width, height: height, adjustHeight: adjustHeight, adjustWidth: adjustWidth, state: window.windowState }; } function delay (f, t) setTimeout(f, t || 0); function refreshWindow () { // FIXME let old = window.outerWidth; window.outerWidth = old + 1; window.outerWidth = old; } function getHideChrome () s2b(mainWindow.getAttribute('hidechrome'), false); function hideChrome (hide, maximize) { hide = !!hide; if (getHideChrome() === hide) return; if (hide) windowInfo = getWindowInfo(); mainWindow.setAttribute('hidechrome', hide); delay(function () { window.outerWidth = windowInfo.width; window.outerHeight = windowInfo.height + windowInfo.adjustHeight; }); if (maximize && windowInfo.state == window.STATE_MAXIMIZED) delay(function () window.maximize()); refreshWindow(); } function setFullscreen (full) { full = !!full; if (full === !!window.fullScreen) return; window.fullScreen = full; delay(function () { hideTargets(full); document.getElementById('status-bar').setAttribute('moz-collapsed', options.get('mainecoon').has('l')); document.getElementById('navigator-toolbox').collapsed = full; if (!full) window.maximize(); }, 1000); // FIXME } function nothing (value) (value === undefined); function important (style) style.replace(/(!important)?\s*;/g, ' !important;'); let echo = (function () { let time = 40; let remove; return function (message) { if (remove) remove(); let doc = window.content.document; // XXX 中身なしっぽいときは、あきらめる。代替手段が欲しい if (!doc.body) return; let style = 'opacity: 1; ' + important( highlight.get('StatusLine').value + U.toStyleText({ position: 'fixed', zIndex: 1000, left: 0, bottom: 0, MozBoxSizing: 'content-box', }) + elemStyle ); let elem = U.xmlToDom(<div id="liberator_maine_coon" style={style}>{message}</div>, doc); doc.body.appendChild(elem); let count = time; let handle = setInterval(function () { try { if (count <= 0) { if (remove) remove(); } else { elem.style.MozOpacity = count / time; } count--; } catch (e) { // XXX ほんとは DOM 関連だけキャッチしたい remove(true); liberator.log(e); } }, 100); remove = function (noDOM) { if (!noDOM) doc.body.removeChild(elem); clearInterval(handle); remove = null; }; }; })(); let setAutoHideCommandLine = (function () { let hiddenNodes = []; return function (hide) { hide = !!hide; if (hide === autoHideCommandLine) return; autoHideCommandLine = hide; if (hide) { let cs = messageBox.parentNode.childNodes; hiddenNodes = []; for (let i = 0, l = cs.length, c; i < l; i++) { c = cs[i]; if (c.id == 'liberator-commandline') continue; let style = window.getComputedStyle(c, ''); hiddenNodes.push([c, c.collapsed, style.display]); if (c.id != 'liberator-message') c.style.display = 'none'; c.collapsed = true; } } else { hiddenNodes.forEach(function ([c, v, d]) [c.collapsed, c.style.display] = [v, d]); } } })(); let useEcho = false; let autoHideCommandLine = false; let displayURL = true; let inputting = false; let windowInfo = {}; { let a = liberator.globalVariables.maine_coon_auto_hide; let d = liberator.globalVariables.maine_coon_default; let def = !nothing(d) ? d : nothing(a) ? 'amu' : s2b(a) ? 'amu' : 'm'; autocommands.add( 'VimperatorEnter', /.*/, function () delay(function () options.get('mainecoon').set(def), 1000) ); } { let last; messageBox.watch('value', function (name, oldValue, newValue) { try { if (autoHideCommandLine && useEcho && /\S/.test(newValue) && messageBox.collapsed && last != newValue && newValue != 'Press ENTER or type command to continue') { echo(newValue); } } catch (e) { liberator.reportError(e); } last = newValue; return newValue; }); } { // for multiline input let cmdline = document.getElementById("liberator-commandline"); messageBox.inputField.__defineGetter__("scrollWidth", function() { return cmdline.clientWidth; }); } U.around(commandline, 'input', function (next, args) { let result = next(); inputting = true; return result; }); U.around(commandline, 'open', function (next, args) { messageBox.collapsed = false; return next(); }); U.around(commandline, 'close', function (next, args) { if (autoHideCommandLine && !inputting) messageBox.collapsed = true; return next(); }); U.around(commandline._callbacks.submit, modes.EX, function (next, args) { let r = next(); if (autoHideCommandLine && !inputting && !(modes.extended & modes.OUTPUT_MULTILINE)) commandline.close(); return r; }); let (callback = function (next) (inputting = false, next())) { U.around(commandline._callbacks.submit, modes.PROMPT, callback); U.around(commandline._callbacks.cancel, modes.PROMPT, callback); } events.addSessionListener( document.getElementById("appcontent"), "DOMContentLoaded", function (event) { let doc = event.originalTarget; if (doc instanceof HTMLDocument && !doc.defaultView.frameElement && displayURL) echo(doc.location.href); }, true ); options.add( ['mainecoon'], 'Make big screen like a Maine Coon', 'charlist', '', { setter: function (value) { function has (c) (value.indexOf(c) >= 0); document.getElementById('status-bar').setAttribute('moz-collapsed', has('l')); if (has('f')) { hideChrome(false); delay(function () setFullscreen(true)); } else if (has('c')) { setFullscreen(false); delay(function () hideChrome(true)); } else if (has('C')) { setFullscreen(false); delay(function () hideChrome(true, true)); } else { hideChrome(false); delay(function () setFullscreen(false)); } setAutoHideCommandLine(has('a')); useEcho = has('m'); displayURL = has('u'); return value; }, completer: function (context, args) { context.title = ['Value', 'Description']; context.completions = [ ['c', 'Hide caption bar'], ['f', 'Fullscreen'], ['a', 'Hide automatically command-line'], ['C', 'Hide caption bar (maximize)'], ['m', 'Displays the message to command-line'], ['l', 'Large mode. Hide status-line'], ['u', 'Displays the message of current page URL when page is loaded.'], ]; }, validater: function (value) /^[cfa]*$/.test(value) } ); // XXX obsolete commands.addUserCommand( ['fullscreen', 'fs'], 'Toggle fullscreen mode', function () setFullscreen(!window.fullScreen), {}, true ); })(); // vim:sw=2 ts=2 et si fdm=marker: