aboutsummaryrefslogtreecommitdiffstats
path: root/autofocus_canceller.js
blob: 3ffa56362e9db5e91b0d2cd53c7af2bce1594b29 (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
// Vimperator Plugin: Auto-Focus Canceller
// Version: 0.1

(function(){

const DEBUG = false;
var org_focus = {};

function disable_focus(){
    var doc = content.document;

    var input = doc.getElementsByTagName("input");
    if(input.length > 0){
        input = input[0];
        org_focus.input = input.wrappedJSObject.__proto__.focus;
        input.wrappedJSObject.__proto__.focus = function(){};
    }

    var textarea = doc.getElementsByTagName("textarea");
    if(textarea.length > 0){
        textarea = textarea[0];
        org_focus.textarea = textarea.wrappedJSObject.__proto__.focus;
        textarea.wrappedJSObject.__proto__.focus = function(){};
    }
}

function enable_focus(){
    var doc = content.document;

    if(org_focus.input){
        var input = doc.getElementsByTagName("input");
        if(input.length > 0){
            input = input[0];
            input.wrappedJSObject.__proto__.focus = org_focus.input;
        }
    }

    if(org_focus.textarea){
        var textarea = doc.getElementsByTagName("textarea");
        if(textarea.length > 0){
            textarea = textarea[0];
            textarea.wrappedJSObject.__proto__.focus = org_focus.textarea;
        }
    }

    org_focus = {};
}

liberator.autocommands.add("PageLoad",
    ".*",
    ":autofocuscanceller"
);

liberator.commands.addUserCommand(
    ["autofocuscanceller"],
    "",
    function(){
        disable_focus();
        content.window.addEventListener("load", function(){
            setTimeout(function(){
                enable_focus();
            }, 1000);
        }, false);
    },
    null, true
);

if(DEBUG){
    liberator.commands.addUserCommand(
        ["disablefocus"],
        "",
        function(){
            disable_focus();
        },
        null, true
    );

    liberator.commands.addUserCommand(
        ["enablefocus"],
        "",
        function(){
            enable_focus();
        },
        null, true
    );
}

})();