Advertisement
Guest User

nodejs Sticky Parameters

a guest
Mar 5th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * External Link Parameter
  3.  *  originally a Typo3 plugin in PHP and JS
  4.  *  - rewritten node-ishly by DukeDiamond for use with a bundler such as Browserify
  5.  *
  6.  * @license http://www.gnu.org/licenses/gpl.txt
  7.  */
  8.  
  9. "use strict";
  10.  
  11. var Uri = require("jsuri");
  12. var $ = $ || global.jQuery;
  13.  
  14.  var StickyParameters = function ( listOfParameters, listOfDomains, applyToInternalLinks, applyToExternalLinks, doNotUseBaseUrl ) {
  15.     // Computes applicableParameters to add
  16.     var parameters;
  17.     this.applicableParameters = listOfParameters.split ? listOfParameters.split(',') : listOfParameters instanceof Array && listOfParameters || [];
  18.     this.applicableDomains = listOfDomains.split ? listOfDomains.split(',') : listOfDomains instanceof Array && listOfDomains || [];
  19.     this.applyToInternalLinks = applyToInternalLinks || false;
  20.     this.applyToExternalLinks = applyToExternalLinks || false;
  21.     this.useBaseUrl = !doNotUseBaseUrl;
  22.  
  23.     parameters = this.getApplicableParameters(global.location.href);
  24.  
  25.     // Only work if there is something to do
  26.     if (parameters.length > 0) {
  27.  
  28.         var newUrl;
  29.         this.baseUrl = global.location.protocol + '//' + global.location.host + '/';
  30.  
  31.         $('a').each(this.initAnchors(this, parameters, newUrl));
  32.     }
  33.  };
  34.  
  35.  StickyParameters.prototype.schemaBlacklist = [
  36.         "javascript:",
  37.         "tel:",
  38.         "mailto:",
  39.  ];
  40.  
  41.  StickyParameters.prototype.initAnchors = function ( _this, parameters, newUrl ) {
  42.      _this = _this || this;
  43.      return function () {
  44.          var uri,
  45.              href = $(this).attr("href") || "";
  46.  
  47.          // true means no local domain has been prepended and one should be added -> most probably internal url
  48.          // added check to see if the uri schema is blacklisted and should not be manipulated
  49.          if (_this.baseUrl && href.indexOf('http') === -1 && !_this.isBlacklistedSchema(href)) {
  50.              href = _this.baseUrl + href;
  51.          }
  52.  
  53.          uri = new Uri(href);
  54.          newUrl = _this.getHref(uri, parameters);
  55.  
  56.          if (newUrl) {
  57.              $(this).attr('href', newUrl);
  58.          }
  59.      };
  60.  };
  61.  
  62.  StickyParameters.prototype.isBlacklistedSchema = function (url) {
  63.      var isBlacklisted = false;
  64.      for (var index in this.schemaBlacklist) {
  65.          if (url.indexOf(this.schemaBlacklist[index]) === 0) isBlacklisted = true;
  66.          else continue;
  67.      }
  68.      return isBlacklisted;
  69.  };
  70.  
  71.  StickyParameters.prototype.isInternalDomain = function ( uri ) {
  72.     return global.location.host === uri.host() || global.location.hostname === uri.host();
  73.  };
  74.  
  75.  StickyParameters.prototype.isExternalDomain = function ( uri ) {
  76.     return global.location.host !== uri.host() && global.location.hostname !== uri.host();
  77.  };
  78.  
  79.  StickyParameters.prototype.getApplicableParameters = function ( path ) {
  80.     var parameters = [],
  81.         uri = new Uri(path),
  82.         applicableParameters = this.applicableParameters;
  83.  
  84.     $.each(applicableParameters, function (index) {
  85.         var parameter = applicableParameters[index];
  86.         if (uri.getQueryParamValue(parameter)) {
  87.             parameters.push({
  88.                 "parameter": parameter,
  89.                 "value": uri.getQueryParamValue(parameter)
  90.             });
  91.         }
  92.     });
  93.  
  94.     return parameters;
  95.  };
  96.  
  97.  StickyParameters.prototype.getHref = function ( uri, parameters ) {
  98.     var newUrl,
  99.         process = true;
  100.  
  101.     // Check if there is a domain restriction
  102.     if ((this.isInternalDomain(uri) && !this.applyToInternalLinks) ||
  103.             (this.isExternalDomain(uri) && !this.applyToExternalLinks)) {
  104.         process = false;
  105.     }
  106.  
  107.     // Check if the external domain is white listed
  108.     if (this.isExternalDomain(uri) &&
  109.             !this.inArray(uri.host(), this.applicableDomains) &&
  110.             this.applicableDomains.length > 0) {
  111.         process = false;
  112.     }
  113.  
  114.     if (process) {
  115.         $.each(parameters, function () {
  116.             uri.replaceQueryParam(this.parameter, this.value);
  117.         });
  118.  
  119.         // remove baseUrl if one and if not unset via useBaseUrl=false
  120.         newUrl = uri.toString();
  121.         if (this.useBaseUrl && this.baseUrl) {
  122.             newUrl = newUrl.replace(this.baseUrl, '');
  123.         }
  124.     }
  125.  
  126.     return newUrl;
  127.  };
  128.  
  129.  StickyParameters.prototype.inArray = function ( needle, haystack ) {
  130.     var length = haystack.length;
  131.     for (var i = 0; i < length; i++) {
  132.         if (haystack[i] === needle) {
  133.             return true;
  134.         }
  135.     }
  136.     return false;
  137.  };
  138.  
  139. module.exports = StickyParameters;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement