1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 var rules = document.getElementById('rules');
7 this.node = document.getElementById('rule-template').cloneNode(true);
8 this.node.id = 'rule' + (Rule.next_id++);
10 rules.appendChild(this.node);
11 this.node.hidden = false;
14 this.getElement('matcher').value = data.matcher;
15 this.getElement('match-param').value = data.match_param;
16 this.getElement('action').value = data.action;
17 this.getElement('action-js').value = data.action_js;
18 this.getElement('enabled').checked = data.enabled;
21 this.getElement('enabled-label').htmlFor = this.getElement('enabled').id =
22 this.node.id + '-enabled';
26 this.getElement('matcher').onchange = storeRules;
27 this.getElement('match-param').onkeyup = storeRules;
28 this.getElement('action').onchange = storeRules;
29 this.getElement('action-js').onkeyup = storeRules;
30 this.getElement('enabled').onchange = storeRules;
33 this.getElement('move-up').onclick = function() {
34 var sib = rule.node.previousSibling;
35 rule.node.parentNode.removeChild(rule.node);
36 sib.parentNode.insertBefore(rule.node, sib);
39 this.getElement('move-down').onclick = function() {
40 var parentNode = rule.node.parentNode;
41 var sib = rule.node.nextSibling.nextSibling;
42 parentNode.removeChild(rule.node);
44 parentNode.insertBefore(rule.node, sib);
46 parentNode.appendChild(rule.node);
50 this.getElement('remove').onclick = function() {
51 rule.node.parentNode.removeChild(rule.node);
57 Rule.prototype.getElement = function(name) {
58 return document.querySelector('#' + this.node.id + ' .' + name);
61 Rule.prototype.render = function() {
62 this.getElement('move-up').disabled = !this.node.previousSibling;
63 this.getElement('move-down').disabled = !this.node.nextSibling;
64 this.getElement('action-js').style.display =
65 (this.getElement('action').value == 'js') ? 'block' : 'none';
70 function loadRules() {
71 var rules = localStorage.rules;
73 JSON.parse(rules).forEach(function(rule) {new Rule(rule);});
75 localStorage.rules = JSON.stringify([]);
79 function storeRules() {
80 localStorage.rules = JSON.stringify(Array.prototype.slice.apply(
81 document.getElementById('rules').childNodes).map(function(node) {
83 return {matcher: node.rule.getElement('matcher').value,
84 match_param: node.rule.getElement('match-param').value,
85 action: node.rule.getElement('action').value,
86 action_js: node.rule.getElement('action-js').value,
87 enabled: node.rule.getElement('enabled').checked};
91 window.onload = function() {
93 document.getElementById('new').onclick = function() {