-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.js
executable file
·155 lines (138 loc) · 4.89 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
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
var attributeNameMap = {
'className': 'className',
'for': 'htmlFor',
html: 'innerHTML',
text: 'textContent'
};
var Element = {
create: function ( tag, props, children ) {
var el = document.createElement( tag );
if ( props ) {
Element.setAttributes( el, props );
}
if ( children ) {
for ( var i = 0, l = children.length; i < l; i += 1 ) {
el.appendChild( children[i] );
}
}
return el;
},
setAttributes: function ( el, props ) {
for ( var prop in props ) {
if ( props.hasOwnProperty( prop ) ) {
var name = attributeNameMap[ prop ],
value = props[ prop ];
if ( value ) {
if ( name ) {
el[ name ] = value;
} else {
el.setAttribute( prop, value );
}
}
}
}
}
};
window.addEventListener( 'DOMContentLoaded', function () {
var storage = localStorage;
function optionChanged( e ) {
var element = e.currentTarget;
var name = element.name;
var value = element.value;
// Set value
storage.setItem( name, value );
}
if( storage.getItem( 'https' ) === '1' ) {
document.getElementById( 'httpsOn' ).checked = 'checked';
} else {
document.getElementById( 'httpsOff' ).checked = 'checked';
}
var optList = document.getElementById( 'optionsList' );
var homeLangSelect = document.getElementById( 'homeLang' ),
browserLang = window.navigator.language,
homeLang = storage.getItem( 'homeLang' ) ||
( Translate.languages[ browserLang ] ? browserLang : 'en' );
// 1. Create the options
var homeLangOptions = document.createDocumentFragment();
var optListOptions = document.createDocumentFragment();
for ( var lang in Translate.languages ) {
var pref = storage.getItem( lang ) || 'ask',
language = Translate.languages[ lang ];
homeLangOptions.appendChild( Element.create( 'option', {
text: language,
value: lang,
selected: homeLang === lang ? 'selected' : null
}) );
var optionsSelect, langSettings = Element.create( 'fieldset', null, [
Element.create( 'label', {
text: language,
className: 'optionlabel'
}),
optionsSelect = Element.create( 'select', {
name: lang
}, [
Element.create( 'option', {
text: 'Always translate ' + language,
value: 'always',
selected: pref === 'always' ? 'selected' : null
}), Element.create( 'option', {
text: 'Ask before translating ' + language,
value: 'ask',
selected: pref === 'ask' ? 'selected' : null
}), Element.create( 'option', {
text: 'Never ask to translate ' + language,
value: 'never',
selected: pref === 'never' ? 'selected' : null
})
])
]);
// listen for changes
optionsSelect.addEventListener( 'change', optionChanged, true );
optListOptions.appendChild( langSettings );
}
homeLangSelect.appendChild( homeLangOptions );
optList.appendChild( optListOptions );
homeLangSelect.addEventListener( 'change', function() {
opera.extension.postMessage({
action: 'setHomeLang',
data: homeLangSelect.value
});
}, false );
document.getElementById( 'clearPreferences' ).addEventListener(
'click', function () {
opera.extension.postMessage({
action: 'resetPreferences'
});
}, false );
document.getElementById( 'blanketPreferences' ).addEventListener(
'click', function () {
opera.extension.postMessage({
action: 'blanketPreferences',
data: {
value: 'always'
}
});
}, false );
document.getElementById( 'blanketPreferencesOff' ).addEventListener(
'click', function () {
opera.extension.postMessage({
action: 'blanketPreferences',
data: {
value: 'never'
}
});
}, false );
document.getElementById( 'httpsOn' ).addEventListener(
'change', function () {
storage.setItem( 'https', '1' );
}, false );
document.getElementById( 'httpsOff' ).addEventListener(
'change', function () {
storage.setItem( 'https', '0' );
}, false );
opera.extension.addEventListener( 'message', function ( msg ) {
if ( msg.data.action === 'preferencesReset' ) {
window.location.reload();
}
}, false );
}, false );