-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.js
127 lines (108 loc) · 3.75 KB
/
options.js
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
var hide_timeout;
var rewriteDefTemplate;
$( document ).ready( function() { restore(); } );
function hide_message() {
clearTimeout( hide_timeout );
$( "#message" ).html( " " ).removeClass();
}
function show_message(type, text, time) {
$( "#message" ).removeClass().addClass( type ).html( text );
hide_timeout = setTimeout( function() { hide_message(); }, time * 1000);
}
// Rewrite definitions are keyed "rewriteDefX", where X is a number starting at 0
// A rewrite definition contain the following fields:
// .regex - the regex to match
// .groupNum - the group number to replace
// .replacement - the replacement text
function restore() {
$( "#addBtn" ).click( add_row );
$( "#message" ).click( hide_message );
$( "#saveBtn" ).click( save );
$( "#testInputText" ).on( 'input', updateTestOutput );
$( window ).unload( save );
rewriteDefTemplate = $( "#rewriteDefTemplate" ).detach();
rewriteDefTemplate.attr( "id", "" );
chrome.storage.sync.get( function( items ) {
for( var key in items ) {
add_row( null, key, items[key] );
}
if( Object.keys( items ).length === 0 ) {
add_row( null, null, null );
show_message( "info", "Add a rewrite rule below.", 15 );
}
});
}
function add_row( event, key, item ) {
var rewriteDef = rewriteDefTemplate.clone();
if( item )
{
rewriteDef.find( "[name='regexText']" ).val( item.regex );
rewriteDef.find( "[name='groupNumText']" ).val( item.groupNum );
rewriteDef.find( "[name='replacementText']" ).val( item.replacement );
rewriteDef.find( "[name='removeBtn']" ).attr( "name", "remove-" + key );
}
rewriteDef.find( "button[name^='remove']" ).click( del_row );
$( "#rewriteDefContainer" ).append( rewriteDef );
}
function del_row() {
var rewriteDef = $( this ).parents( ".rewriteDef" );
rewriteDef.remove();
var key = rewriteDef.find( "button[name^='remove']" ).attr( "name" ).split( "-" )[1];
if( key ) chrome.storage.sync.remove( key, function() {
if( runtime.lastError ) show_message( "error", "Failed to remove rewrite rule: " + runtime.lastError, 10 );
else show_message( "success", "Removed rewrite rule.", 5 );
} );
}
function save() {
var store = {};
var i = 0;
var unusedFields = false;
var rewriteDefs = $( ".rewriteDef" ).each( function() {
var item = {
regex: $( this ).find( "[name='regexText']" ).val(),
groupNum: $( this ).find( "[name='groupNumText']" ).val(),
replacement: $( this ).find( "[name='replacementText']" ).val(),
};
if( item.regex.length == 0 || item.groupNum.length == 0 || item.replacement.length == 0 )
{
show_message( "warning", "Please make sure all fields are filled in before saving. Delete any unused rows.", 5 );
unusedFields = true;
return false;
}
store[i++] = item;
} );
if( unusedFields ) return;
chrome.storage.sync.get(function(items) {
for (var key in items) {
if (!store.hasOwnProperty(key)) {
chrome.storage.sync.remove(key);
}
}
});
chrome.storage.sync.set(store);
show_message("success", "Saved all rewrite rules.", 5);
}
function updateTestOutput()
{
var testInput = $( this ).val();
var foundMatch = false;
var rewriteDefs = $( ".rewriteDef" ).each( function() {
var item = {
regex: $( this ).find( "[name='regexText']" ).val(),
groupNum: $( this ).find( "[name='groupNumText']" ).val(),
replacement: $( this ).find( "[name='replacementText']" ).val(),
};
if( item.regex.length > 0 && item.groupNum.length > 0 && item.replacement.length > 0 )
{
var match = testInput.match( new RegExp( item.regex, "i" ) );
if( match && match.length > item.groupNum )
{
match[item.groupNum] = item.replacement;
$( "#testOutputText" ).val( match.slice( 1 ).join( "" ) );
foundMatch = true;
return false;
}
}
} );
if( !foundMatch ) $( "#testOutputText" ).val( "(no match found)" );
}