Conditions | 2 |
Paths | 1 |
Total Lines | 234 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | $(document).ready(function () { |
||
2 | var _this = this; |
||
3 | var storage = new API.Storage(); |
||
4 | |||
5 | |||
6 | function fillLogin(login) { |
||
7 | API.runtime.sendMessage(API.runtime.id, { |
||
8 | method: 'passToParent', |
||
9 | args: { |
||
10 | injectMethod: 'enterLoginDetails', |
||
11 | args: login |
||
12 | } |
||
13 | }); |
||
14 | } |
||
15 | function removePasswordPicker(login) { |
||
|
|||
16 | API.runtime.sendMessage(API.runtime.id, { |
||
17 | method: 'passToParent', |
||
18 | args: { |
||
19 | injectMethod: 'removePasswordPicker' |
||
20 | } |
||
21 | }); |
||
22 | } |
||
23 | |||
24 | function copyTextToClipboard(text) { |
||
25 | var copyFrom = document.createElement("textarea"); |
||
26 | copyFrom.textContent = text; |
||
27 | var body = document.getElementsByTagName('body')[0]; |
||
28 | body.appendChild(copyFrom); |
||
29 | copyFrom.select(); |
||
30 | document.execCommand('copy'); |
||
31 | body.removeChild(copyFrom); |
||
32 | } |
||
33 | |||
34 | _this.copyTextToClipboard = copyTextToClipboard; |
||
35 | |||
36 | |||
37 | function setupAddCredentialFields() { |
||
38 | var labelfield = $('#savepw-label'); |
||
39 | labelfield.val(document.title); |
||
40 | var userfield = $('#savepw-username'); |
||
41 | var pwfield = $('#savepw-password'); |
||
42 | $('.togglePw').click(function () { |
||
43 | $('.togglePw').find('.fa').toggleClass('fa-eye').toggleClass('fa-eye-slash'); |
||
44 | if (pwfield.attr('type') === 'password') { |
||
45 | pwfield.attr('type', 'text'); |
||
46 | } else { |
||
47 | pwfield.attr('type', 'password'); |
||
48 | } |
||
49 | }); |
||
50 | |||
51 | $('#savepw-save').click(function (e) { |
||
52 | e.preventDefault(); |
||
53 | API.runtime.sendMessage(API.runtime.id, { |
||
54 | method: "injectCreateCredential", args: { |
||
55 | label: labelfield.val(), |
||
56 | username: userfield.val(), |
||
57 | password: pwfield.val() |
||
58 | } |
||
59 | }); |
||
60 | }); |
||
61 | |||
62 | $('#savepw-cancel').click(function () { |
||
63 | labelfield.val(document.title); |
||
64 | userfield.val(''); |
||
65 | pwfield.val(''); |
||
66 | removePasswordPicker(); |
||
67 | //removePasswordPicker(); |
||
68 | }); |
||
69 | |||
70 | } |
||
71 | |||
72 | function toggleFieldType(field) { |
||
73 | if ($(field).attr('type').toLowerCase() === 'text') { |
||
74 | $(field).attr('type', 'password'); |
||
75 | } else { |
||
76 | $(field).attr('type', 'text'); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | function genPwd(settings) { |
||
81 | /* jshint ignore:start */ |
||
82 | var password = generatePassword(settings['length'], |
||
83 | settings.useUppercase, |
||
84 | settings.useLowercase, |
||
85 | settings.useDigits, |
||
86 | settings.useSpecialChars, |
||
87 | settings.minimumDigitCount, |
||
88 | settings.avoidAmbiguousCharacters, |
||
89 | settings.requireEveryCharType); |
||
90 | /* jshint ignore:end */ |
||
91 | return password; |
||
92 | } |
||
93 | |||
94 | function getPasswordGenerationSettings(cb) { |
||
95 | var default_settings = { |
||
96 | 'length': 12, |
||
97 | 'useUppercase': true, |
||
98 | 'useLowercase': true, |
||
99 | 'useDigits': true, |
||
100 | 'useSpecialChars': true, |
||
101 | 'minimumDigitCount': 3, |
||
102 | 'avoidAmbiguousCharacters': false, |
||
103 | 'requireEveryCharType': true |
||
104 | }; |
||
105 | storage.get('password_generator_settings').then(function (_settings) { |
||
106 | if (!_settings) { |
||
107 | _settings = default_settings; |
||
108 | } |
||
109 | |||
110 | cb(_settings); |
||
111 | }).error(function () { |
||
112 | cb(default_settings); |
||
113 | }); |
||
114 | } |
||
115 | |||
116 | function setupPasswordGenerator() { |
||
117 | //getPasswordGeneratorSettings |
||
118 | getPasswordGenerationSettings(function (settings) { |
||
119 | var round = 0; |
||
120 | |||
121 | function generate_pass(inputId) { |
||
122 | var new_password = genPwd(settings); |
||
123 | $('#'+ inputId).val(new_password); |
||
124 | setTimeout(function () { |
||
125 | if (round < 10) { |
||
126 | generate_pass(inputId); |
||
127 | round++; |
||
128 | } else { |
||
129 | round = 0; |
||
130 | } |
||
131 | }, 10); |
||
132 | } |
||
133 | |||
134 | $.each(settings, function (setting, val) { |
||
135 | if (typeof(val) === "boolean") { |
||
136 | $('[name="' + setting + '"]').prop('checked', val); |
||
137 | } else { |
||
138 | $('[name="' + setting + '"]').val(val); |
||
139 | } |
||
140 | }); |
||
141 | |||
142 | $('form[name="advancedSettings"]').change(function () { |
||
143 | var pw_settings_form = $(this); |
||
144 | settings = pw_settings_form.serializeObject(); |
||
145 | storage.set('password_generator_settings', settings); |
||
146 | }); |
||
147 | |||
148 | $('.renewpw').click(function () { |
||
149 | generate_pass('generated_password'); |
||
150 | }); |
||
151 | $('.renewpw_newac').click(function () { |
||
152 | generate_pass('savepw-password'); |
||
153 | |||
154 | }); |
||
155 | $('.renewpw').click(); |
||
156 | $('.renewpw_newac').click(); |
||
157 | |||
158 | $('.usepwd').click(function () { |
||
159 | $('#savepw-password').val($('#generated_password').val()); |
||
160 | $('.tab.add').click(); |
||
161 | }); |
||
162 | |||
163 | $('.togglePwVis').click(function () { |
||
164 | toggleFieldType('#generated_password'); |
||
165 | $(this).find('.fa').toggleClass('fa-eye-slash').toggleClass('fa-eye'); |
||
166 | }); |
||
167 | |||
168 | $('.adv_opt').click(function () { |
||
169 | |||
170 | var adv_settings = $('.pw-setting-advanced'); |
||
171 | $(this).find('i').toggleClass('fa-angle-right').toggleClass('fa-angle-down'); |
||
172 | if (adv_settings.is(':visible')) { |
||
173 | adv_settings.slideUp(); |
||
174 | } else { |
||
175 | adv_settings.slideDown(); |
||
176 | } |
||
177 | }); |
||
178 | }); |
||
179 | } |
||
180 | |||
181 | var picker = $('#password_picker'); |
||
182 | picker.find('.tab').click(function () { |
||
183 | var target = $(this).attr('class').replace('active', '').replace('tab', '').trim(); |
||
184 | picker.find('.tab').removeClass('active'); |
||
185 | picker.find('.tab-content').children().hide(); |
||
186 | picker.find('.tab-' + target + '-content').show(); |
||
187 | picker.find('.tab.' + target).addClass('active'); |
||
188 | }); |
||
189 | |||
190 | $('.tab.close').click(function () { |
||
191 | removePasswordPicker(); |
||
192 | }); |
||
193 | |||
194 | var url = (window.location != window.parent.location) |
||
195 | ? document.referrer |
||
196 | : document.location.href; |
||
197 | API.runtime.sendMessage(API.runtime.id, {method: "getCredentialsByUrl", args: [url]}).then(function (logins) { |
||
198 | if (logins.length !== 0) { |
||
199 | picker.find('.tab-list-content').html(''); |
||
200 | } |
||
201 | for (var i = 0; i < logins.length; i++) { |
||
202 | var login = logins[i]; |
||
203 | var div = $('<div>', {class: 'account', text: login.label}); |
||
204 | $('<br>').appendTo(div) |
||
205 | $('<small>').text(login.username).appendTo(div) |
||
206 | /* jshint ignore:start */ |
||
207 | div.click((function (login) { |
||
208 | return function () { |
||
209 | //enterLoginDetails(login); |
||
210 | //API.runtime.sendMessage(API.runtime.id, {method: 'getMasterPasswordSet'}) |
||
211 | fillLogin(login) |
||
212 | }; |
||
213 | })(login)); |
||
214 | /* jshint ignore:end*/ |
||
215 | |||
216 | picker.find('.tab-list-content').append(div); |
||
217 | } |
||
218 | }); |
||
219 | $('.no-credentials .save').on('click', function () { |
||
220 | $('.tab.add').click(); |
||
221 | }); |
||
222 | $('.no-credentials .gen').on('click', function () { |
||
223 | $('.tab.generate').click(); |
||
224 | }); |
||
225 | setupAddCredentialFields(); |
||
226 | setupPasswordGenerator(); |
||
227 | |||
228 | |||
229 | API.runtime.onMessage.addListener(function (msg, sender, sendResponse) { |
||
230 | if (_this[msg.method]) { |
||
231 | _this[msg.method](msg.args, sender); |
||
232 | } |
||
233 | }); |
||
234 | }); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.