blocklinks.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * WARNING!!!
  3. *
  4. * The below code is tricky and hacky. It is meant as a temporary solution
  5. * to block outgoing links within HTML5 games. It is strongly adviced to not
  6. * use this script, unless you know exactly what you are doing.
  7. */
  8. (function (w, d) {
  9. 'use strict';
  10. var blockList = [508], // site IDs for which you'd like to block the outgoing links
  11. addEventListener = (function () {
  12. var overwrite;
  13. if (w.addEventListener) {
  14. overwrite = function (type, listener, element) {
  15. element.addEventListener(type, listener, false);
  16. };
  17. } else if (w.attachEvent) {
  18. overwrite = function (type, listener, element) {
  19. element.attachEvent('on' + type, listener);
  20. };
  21. }
  22. return overwrite;
  23. }()),
  24. preventDefault = function (e) {
  25. if (e.preventDefault) {
  26. e.preventDefault();
  27. }
  28. if (e.stopImmediatePropagation) {
  29. e.stopImmediatePropagation();
  30. }
  31. if (e.stopPropagation) {
  32. e.stopPropagation();
  33. }
  34. if (e.stop) {
  35. e.stop();
  36. }
  37. e.returnValue = false;
  38. return false;
  39. },
  40. blockOutgoingLinks = function () {
  41. var nativeFunctions = {
  42. 'createElement': d.createElement,
  43. 'open': w.open
  44. };
  45. /**
  46. * Overwrite the native functions so we can intercept the calls to them
  47. */
  48. // func signature: window.open(strUrl, strWindowName[, strWindowFeatures]);
  49. d.createElement = function (tagName) {
  50. var elementFromMemory = nativeFunctions.createElement.apply(this, arguments);
  51. if (tagName && 'a' === tagName) {
  52. addEventListener('click', function (e) {
  53. preventDefault(e);
  54. }, elementFromMemory);
  55. }
  56. return elementFromMemory;
  57. };
  58. // func signature: window.open(strUrl, strWindowName[, strWindowFeatures]);
  59. w.open = function () {
  60. // just don't do anything
  61. };
  62. },
  63. getUrlParam = function(paramName) {
  64. var keyvaluePairs = w.location.href.slice(w.location.href.indexOf('?') + 1).split('&'),
  65. pairsCount = keyvaluePairs.length,
  66. keyvalueArray,
  67. value,
  68. key,
  69. i;
  70. for (i = 0; i < pairsCount; i += 1) {
  71. keyvalueArray = keyvaluePairs[i].split('=');
  72. key = keyvalueArray[0];
  73. value = keyvalueArray[1];
  74. if (key === paramName) {
  75. return decodeURIComponent(value);
  76. }
  77. }
  78. return null;
  79. },
  80. indexOf = [].indexOf||(function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;}), //polyfill
  81. siteId = parseInt(getUrlParam('siteid'), 10);
  82. // check if we should block the outgoing links based on the site ID query string parameter
  83. if (siteId && indexOf.call(blockList, siteId) > -1) {
  84. blockOutgoingLinks();
  85. }
  86. }(window, window.document));