Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * External Link Parameter
- * originally a Typo3 plugin in PHP and JS
- * - rewritten node-ishly by DukeDiamond for use with a bundler such as Browserify
- *
- * @license http://www.gnu.org/licenses/gpl.txt
- */
- "use strict";
- var Uri = require("jsuri");
- var $ = $ || global.jQuery;
- var StickyParameters = function ( listOfParameters, listOfDomains, applyToInternalLinks, applyToExternalLinks, doNotUseBaseUrl ) {
- // Computes applicableParameters to add
- var parameters;
- this.applicableParameters = listOfParameters.split ? listOfParameters.split(',') : listOfParameters instanceof Array && listOfParameters || [];
- this.applicableDomains = listOfDomains.split ? listOfDomains.split(',') : listOfDomains instanceof Array && listOfDomains || [];
- this.applyToInternalLinks = applyToInternalLinks || false;
- this.applyToExternalLinks = applyToExternalLinks || false;
- this.useBaseUrl = !doNotUseBaseUrl;
- parameters = this.getApplicableParameters(global.location.href);
- // Only work if there is something to do
- if (parameters.length > 0) {
- var newUrl;
- this.baseUrl = global.location.protocol + '//' + global.location.host + '/';
- $('a').each(this.initAnchors(this, parameters, newUrl));
- }
- };
- StickyParameters.prototype.schemaBlacklist = [
- "javascript:",
- "tel:",
- "mailto:",
- ];
- StickyParameters.prototype.initAnchors = function ( _this, parameters, newUrl ) {
- _this = _this || this;
- return function () {
- var uri,
- href = $(this).attr("href") || "";
- // true means no local domain has been prepended and one should be added -> most probably internal url
- // added check to see if the uri schema is blacklisted and should not be manipulated
- if (_this.baseUrl && href.indexOf('http') === -1 && !_this.isBlacklistedSchema(href)) {
- href = _this.baseUrl + href;
- }
- uri = new Uri(href);
- newUrl = _this.getHref(uri, parameters);
- if (newUrl) {
- $(this).attr('href', newUrl);
- }
- };
- };
- StickyParameters.prototype.isBlacklistedSchema = function (url) {
- var isBlacklisted = false;
- for (var index in this.schemaBlacklist) {
- if (url.indexOf(this.schemaBlacklist[index]) === 0) isBlacklisted = true;
- else continue;
- }
- return isBlacklisted;
- };
- StickyParameters.prototype.isInternalDomain = function ( uri ) {
- return global.location.host === uri.host() || global.location.hostname === uri.host();
- };
- StickyParameters.prototype.isExternalDomain = function ( uri ) {
- return global.location.host !== uri.host() && global.location.hostname !== uri.host();
- };
- StickyParameters.prototype.getApplicableParameters = function ( path ) {
- var parameters = [],
- uri = new Uri(path),
- applicableParameters = this.applicableParameters;
- $.each(applicableParameters, function (index) {
- var parameter = applicableParameters[index];
- if (uri.getQueryParamValue(parameter)) {
- parameters.push({
- "parameter": parameter,
- "value": uri.getQueryParamValue(parameter)
- });
- }
- });
- return parameters;
- };
- StickyParameters.prototype.getHref = function ( uri, parameters ) {
- var newUrl,
- process = true;
- // Check if there is a domain restriction
- if ((this.isInternalDomain(uri) && !this.applyToInternalLinks) ||
- (this.isExternalDomain(uri) && !this.applyToExternalLinks)) {
- process = false;
- }
- // Check if the external domain is white listed
- if (this.isExternalDomain(uri) &&
- !this.inArray(uri.host(), this.applicableDomains) &&
- this.applicableDomains.length > 0) {
- process = false;
- }
- if (process) {
- $.each(parameters, function () {
- uri.replaceQueryParam(this.parameter, this.value);
- });
- // remove baseUrl if one and if not unset via useBaseUrl=false
- newUrl = uri.toString();
- if (this.useBaseUrl && this.baseUrl) {
- newUrl = newUrl.replace(this.baseUrl, '');
- }
- }
- return newUrl;
- };
- StickyParameters.prototype.inArray = function ( needle, haystack ) {
- var length = haystack.length;
- for (var i = 0; i < length; i++) {
- if (haystack[i] === needle) {
- return true;
- }
- }
- return false;
- };
- module.exports = StickyParameters;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement