1 var conkeror = Components.classes['@conkeror.mozdev.org/application;1'].getService ().wrappedJSObject;
3 var last_keydown_event = null;
6 function handle_keydown(e) {
10 last_keydown_event = e;
13 function handle_keypress(e) {
17 // Ignore this keypress if we don't have a keycode
18 if (!last_keydown_event)
21 // Ignore this keypress if other modifiers are down
22 if (e.altKey || e.ctrlKey || e.metaKey)
25 // Ignore this keypress if we don't have a charcode
30 if (e.charCode == " ".charCodeAt(0))
33 var shift = e.shiftKey;
35 var keycode = last_keydown_event.keyCode;
36 var charcode = e.charCode;
38 add_mapping(keycode, shift, charcode, true);
41 var unshifted_keycode_to_charcode = [];
42 var shifted_keycode_to_charcode = [];
46 function add_mapping(keycode, shift, charcode, single) {
47 var table = shift ? shifted_keycode_to_charcode : unshifted_keycode_to_charcode;
49 var obj = table[keycode];
51 obj.element.parentNode.removeChild(obj.element);
54 var g = new conkeror.dom_generator(document, conkeror.XUL_NS);
55 var list = document.getElementById("mapping_list");
56 var item = g.element("listitem");
57 g.element("listcell", item, "label", keycode);
58 var keycode_name = conkeror.keycode_to_vk_name[keycode] || "";
59 g.element("listcell", item, "label", keycode_name);
60 g.element("listcell", item, "label", shift ? "true" : "false");
61 g.element("listcell", item, "label", charcode);
62 g.element("listcell", item, "label", String.fromCharCode(charcode));
63 list.appendChild(item);
64 table[keycode] = {charcode: charcode, element: item};
66 list.ensureElementIsVisible(item);
67 message("Added mapping: " + keycode + ((keycode_name.length > 0)? " (" + keycode_name + ")" : "") +
68 " -> " + charcode + " ('" + String.fromCharCode(charcode) + "')");
73 function update_count() {
74 document.getElementById("count").value = "Count: " + count;
79 var tables = [unshifted_keycode_to_charcode, shifted_keycode_to_charcode];
80 for each (let table in tables) {
81 for each (let x in table) {
82 x.element.parentNode.removeChild(x.element);
85 unshifted_keycode_to_charcode = [];
86 shifted_keycode_to_charcode = [];
88 message("Cleared mappings.");
91 function load_tables(tables) {
92 let [unshifted_table, shifted_table] = tables;
94 for (let x in unshifted_table) {
95 let y = unshifted_table[x];
97 add_mapping(x, false, y);
99 for (let x in shifted_table) {
100 let y = shifted_table[x];
102 add_mapping(x, true, y);
107 function load(use_defaults) {
108 let tables = conkeror.get_charcode_mapping_table_from_preferences();
109 if (tables != null) {
111 message("Loaded mappings.");
115 message("No mapping table stored in preferences. The default mapping table was loaded.");
118 message("Error: No mapping table stored in preferences.");
123 var unshifted_table = unshifted_keycode_to_charcode.map(function (x) x.charcode);
124 var shifted_table = shifted_keycode_to_charcode.map(function (x) x.charcode);
126 var str = [unshifted_table, shifted_table].toSource();
127 conkeror.user_pref("conkeror.charCodeMappingTable", str);
128 message("Saved mappings. Restart Conkeror for the new mapping table to take effect.");
132 let tables = conkeror.get_default_keycode_to_charcode_tables();
134 message("Loaded default mapping table.");
137 function clear_preference() {
139 conkeror.clear_pref("conkeror.charCodeMappingTable");
140 message("Cleared preference. Restart Conkeror for the default table to take effect.");
142 message("Preference was already not set.");
146 function message(msg) {
147 document.getElementById("message").value = msg;
151 window.addEventListener("keydown", handle_keydown, true);
152 window.addEventListener("keypress", handle_keypress, true);