Find access to blocked websites https://rsf.org/collateral-freedom

l10n.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. license: The MIT License, Copyright (c) 2016-2017 YUKI "Piro" Hiroshi
  3. original:
  4. http://github.com/piroor/webextensions-lib-l10n
  5. */
  6. var l10n = {
  7. updateString(aString) {
  8. return aString.replace(/__MSG_(.+?)__/g, function(aMatched) {
  9. var key = aMatched.slice(6, -2);
  10. return chrome.i18n.getMessage(key);
  11. });
  12. },
  13. $log(aMessage, ...aArgs) {
  14. aMessage = 'l10s ' + aMessage;
  15. if (typeof log === 'function')
  16. log(aMessage, ...aArgs);
  17. else
  18. console.log(aMessage, ...aArgs);
  19. },
  20. updateDocument() {
  21. var texts = document.evaluate(
  22. 'descendant::text()[contains(self::text(), "__MSG_")]',
  23. document,
  24. null,
  25. XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  26. null
  27. );
  28. for (let i = 0, maxi = texts.snapshotLength; i < maxi; i++)
  29. {
  30. let text = texts.snapshotItem(i);
  31. text.nodeValue = this.updateString(text.nodeValue);
  32. }
  33. var attributes = document.evaluate(
  34. 'descendant::*/attribute::*[contains(., "__MSG_")]',
  35. document,
  36. null,
  37. XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  38. null
  39. );
  40. for (let i = 0, maxi = attributes.snapshotLength; i < maxi; i++)
  41. {
  42. let attribute = attributes.snapshotItem(i);
  43. this.$log('apply', attribute);
  44. attribute.value = this.updateString(attribute.value);
  45. }
  46. }
  47. };
  48. document.addEventListener('DOMContentLoaded', function onReady() {
  49. document.removeEventListener('DOMContentLoaded', onReady);
  50. l10n.updateDocument();
  51. });