| Conditions | 1 |
| Paths | 1 |
| Total Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 | /* global API */ |
||
| 25 | (function () { |
||
| 26 | 'use strict'; |
||
| 27 | var pendingRequests = []; |
||
| 28 | |||
| 29 | API.runtime.connect(); |
||
| 30 | // A request has completed. |
||
| 31 | // We can stop worrying about it. |
||
| 32 | function completed(requestDetails) { |
||
| 33 | var index = pendingRequests.indexOf(requestDetails.requestId); |
||
| 34 | if (index > -1) { |
||
| 35 | pendingRequests.splice(index, 1); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | var auth_tries = []; |
||
| 40 | var provideCredentialsSync = function (requestDetails) { |
||
| 41 | if (!auth_tries[requestDetails.requestId]) { |
||
| 42 | auth_tries[requestDetails.requestId] = 0; |
||
| 43 | } |
||
| 44 | /** global: background */ |
||
| 45 | var login = background.getCredentialForHTTPAuth(requestDetails); |
||
| 46 | |||
| 47 | // If we have seen this request before, then |
||
| 48 | // assume our credentials were bad, and give up. |
||
| 49 | if (pendingRequests.indexOf(requestDetails.requestId) === -1) { |
||
| 50 | pendingRequests.push(requestDetails.requestId); |
||
| 51 | return { |
||
| 52 | authCredentials: { |
||
| 53 | username: (login.username) ? login.username : login.email , |
||
| 54 | password: login.password |
||
| 55 | } |
||
| 56 | }; |
||
| 57 | |||
| 58 | } else { |
||
| 59 | console.warn("bad credentials for: " + requestDetails.url + ', Showing login dialog'); |
||
| 60 | //return {cancel: true}; |
||
| 61 | return undefined; |
||
| 62 | } |
||
| 63 | |||
| 64 | }; |
||
| 65 | |||
| 66 | |||
| 67 | API.webRequest.onAuthRequired.addListener(provideCredentialsSync, {urls: ["<all_urls>"]}, ["blocking"]); |
||
| 68 | |||
| 69 | API.webRequest.onCompleted.addListener( |
||
| 70 | completed, |
||
| 71 | {urls: ["<all_urls>"]} |
||
| 72 | ); |
||
| 73 | |||
| 74 | API.webRequest.onErrorOccurred.addListener( |
||
| 75 | completed, |
||
| 76 | {urls: ["<all_urls>"]} |
||
| 77 | ); |
||
| 78 | }()); |