polyfill.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. let elem = document.createElement('div');
  2. elem.classList.toggle('test-class', false);
  3. if (elem.classList.contains('test-class')) {
  4. let _toggle = DOMTokenList.prototype.toggle;
  5. DOMTokenList.prototype.toggle = function(token, force) {
  6. if (arguments.length > 1 && !this.contains(token) === !force) {
  7. return force;
  8. } else {
  9. return _toggle.call(this, token);
  10. }
  11. };
  12. }
  13. if (!String.prototype.startsWith) {
  14. String.prototype.startsWith = function(searchString, position){
  15. position = position || 0;
  16. return this.substr(position, searchString.length) === searchString;
  17. };
  18. }
  19. if (!String.prototype.endsWith) {
  20. String.prototype.endsWith = function(searchString, position) {
  21. var subjectString = this.toString();
  22. if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
  23. position = subjectString.length;
  24. }
  25. position -= searchString.length;
  26. var lastIndex = subjectString.indexOf(searchString, position);
  27. return lastIndex !== -1 && lastIndex === position;
  28. };
  29. }
  30. if (!Array.prototype.find) {
  31. Object.defineProperty(Array.prototype, "find", {
  32. value: function(predicate) {
  33. if (this === null) {
  34. throw new TypeError('Array.prototype.find called on null or undefined');
  35. }
  36. if (typeof predicate !== 'function') {
  37. throw new TypeError('predicate must be a function');
  38. }
  39. var list = Object(this);
  40. var length = list.length >>> 0;
  41. var thisArg = arguments[1];
  42. var value;
  43. for (var i = 0; i < length; i++) {
  44. value = list[i];
  45. if (predicate.call(thisArg, value, i, list)) {
  46. return value;
  47. }
  48. }
  49. return undefined;
  50. }
  51. });
  52. }
  53. document.addEventListener("DOMContentLoaded", function() {
  54. // Disable resizing in Firefox
  55. document.execCommand("enableObjectResizing", false, false);
  56. // Disable automatic linkifying in IE11
  57. document.execCommand("autoUrlDetect", false, false);
  58. });