-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.html
210 lines (172 loc) · 5.74 KB
/
options.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/options.css">
<script src="js/login.js"></script>
<title id="widget-title">uInstapaper</title>
</head>
<body>
<header>
<img src="icons/big_icon.png">
<h1 id="widget-name">uInstapaper v0.01</h1>
<h2>Made by <span id="widget-author">Arturo Linares</h2>
</header>
<section>
<p>This extension adds the current showing page to Instapaper.</p>
<h3>Instapaper account</h3>
<fieldset>
<p><span id="status"></span></p>
<p>
<label for="username">Username</label>
<input id="username" name="username" type="text"/>
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password"/>
</p>
<div style="text-align:center"><input id="login_btn" type="button" name="login" value="Login" onclick="login()"/></div>
</fieldset>
</section>
<footer>
<p>Powered by the Opera Browser</p>
</footer>
<script>
login = function()
{
opera.postError('Logging in..');
uInstaLogin.loginListener = function()
{
document.getElementById('status').textContent = 'Logged in!';
};
uInstaLogin.errorListener = function()
{
document.getElementById('status').textContent = 'Wrong user name or password.';
};
uInstaLogin.auth();
};
addEventListener
(
'DOMContentLoaded',
function()
{
// storage
var storage = widget.preferences;
// glue for multiple values ( checkbox, select-multiple )
var glue = '\n';
// get the FORM elements
var formElements = document.querySelectorAll( 'input,select' );
// list of FORM elements
var skip = hash( 'hidden,submit,image,reset,button' );
var multipleValues = hash( 'checkbox,select-multiple' );
var checkable = hash( 'checkbox,radio' );
// string to hash
function hash( str, glue )
{
var obj = {};
var tmp = str.split(glue||',');
while( tmp.length )
obj[ tmp.pop() ] = true;
return obj;
}
// walk the elements and apply a callback method to them
function walkElements( callback )
{
var obj = [];
for( var i=0,element=null; element=formElements[i++]; )
{
// skip the element if it has no name or is of a type with no useful value
var type = element.type.toLowerCase();
var name = element.name||'';
if( skip[type]===true || name=='') continue;
var tmp = callback( element, name, type );
if( tmp!=null )
obj.push( tmp );
}
return obj;
}
// listener for element changes
function changedElement( e )
{
var element = e.currentTarget||e;
var type = element.type.toLowerCase();
var name = element.name||'';
var value = multipleValues[type]!==true?element.value:walkElements
(
function( e, n, t )
{
if( n==name && e.options )
{
var tmp = [];
for( var j=0,option=null; option=e.options[j++]; )
{
if( option.selected )
{
tmp.push( option.value );
}
}
return tmp.join( glue );
}
else if( n==name && checkable[t]===true && e.checked )
{
return e.value;
}
}
).join( glue );
// set value
storage.setItem( name, value );
}
// set the textContent of an element
function setText( id, txt )
{
var e = document.getElementById(id);
if( e )
{
e.textContent = txt;
}
}
// populate the title, name, author, ...
setText( 'widget-title', widget.name );
setText( 'widget-name', widget.name );
setText( 'widget-author', widget.author );
// walk and set the elements accordingly to the storage
walkElements
(
function( element, name, type )
{
var value = storage[name]!==undefined?storage.getItem( name ):element.value;
var valueHash = hash( value, glue );
if( element.selectedOptions )
{
// 'select' element
for( var j=0,option=null; option=element.options[j++]; )
{
option.selected = valueHash[option.value]===true;
}
}
else if( checkable[type]===true )
{
// 'checkable' element
element.checked = valueHash[element.value]===true;
}
else
{
// any other kind of element
element.value = value;
}
// set the widget.preferences to the value of the element if it was undefined
// YOU MAY NOT WANT TO DO THIS
if( storage[name]==undefined )
{
changedElement( element );
}
// listen to changes
element.addEventListener( 'change', changedElement, true );
}
);
},
false
);
</script>
</body>
</html>