zxcvbn.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* global zxcvbn */
  2. export class Zxcvbn {
  3. init(include) {
  4. this._include = include;
  5. this._isLoaded = false;
  6. }
  7. scorePassword(password, inputs) {
  8. // 0-4 score, the more the stronger password
  9. if (this._isLoaded) {
  10. return zxcvbn(password, inputs).score;
  11. }
  12. return 0;
  13. }
  14. load() {
  15. if (!this._isLoaded) {
  16. this._include.include('misago/js/zxcvbn.js');
  17. return this._loadingPromise();
  18. } else {
  19. return this._loadedPromise();
  20. }
  21. }
  22. _loadingPromise() {
  23. const self = this;
  24. return new Promise(function(resolve, reject) {
  25. var wait = function(tries=0) {
  26. tries += 1;
  27. if (tries > 200) {
  28. reject();
  29. } else if (typeof zxcvbn === "undefined") {
  30. window.setTimeout(function() {
  31. wait(tries);
  32. }, 200);
  33. } else {
  34. self._isLoaded = true;
  35. resolve();
  36. }
  37. };
  38. wait();
  39. });
  40. }
  41. _loadedPromise() {
  42. // we have already loaded zxcvbn.js, resolve away!
  43. return new Promise(function(resolve) {
  44. resolve();
  45. });
  46. }
  47. }
  48. export default new Zxcvbn();