-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyprefix.min.js
More file actions
20 lines (19 loc) · 4.17 KB
/
Copy pathproxyprefix.min.js
File metadata and controls
20 lines (19 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(function($,window,document,undefined){$.error=function(msg){if(window.console!==undefined){console.error(msg);}};var pluginName="ProxyPrefix";defaults={attribute:'href',protocols:new Array('http','https'),hostnames:new Array(),loginpath:'/login',remove:{'web.ebscohost.com':new Array('sid')},append:{'search.ebscohost.com':{'auth':'uid','user':'libraryresearch','password':'libraryresearch'}}};function Plugin(element,options){this.element=element;this.options=$.extend({},defaults,options);this._defaults=defaults;this._name=pluginName;this.init();}
Plugin.prototype={init:function(){var uri=URI($(this.element).attr(this.options.attribute)).normalize();if(this.validate(this.element,this.options,uri)===false){return false;}
this.remove_param(this.element,this.options,uri);this.append_param(this.element,this.options,uri);var found=false;var options=this.options;$.each(options.hostnames,function(key,value){if(uri.host()===value&&uri.pathname()===options.loginpath){found=true;if(key===0){return false;}else{uri.host(options.hostnames[0]);return false;}}});if(found===true){this.rewrite(this.element,this.options,uri);}else{this.prefix(this.element,this.options,uri);}
return this;},validate:function(el,options,uri){if(uri.protocol()===''){uri.protocol('http');}
if(this.options['protocols'].indexOf(uri.protocol())<0){$.error('Unsupported protocol: '+uri.protocol());return false;}
if(uri.is('urn')===true){$.error('The proxy address must be a URL. A URN was given instead.');return false;}
if(uri.is('IP')||uri.is('IPv4')||uri.is('IPv6')){$.error('The proxy address must be a hostname. '+'IP addresses are not valid.');return false;}
return true;},remove_param:function(el,options,uri){try{$.each(this.options.remove[uri.host()],function(key,value){if(uri.hasSearch(value)){uri.removeSearch(value);}});}catch(err){}
$(el).attr(options.attribute,uri.href());},append_param:function(el,options,uri){try{$.each(this.options.append[uri.host()],function(key,value){if(uri.hasSearch(key)===false){uri.addSearch(key,value);}});}catch(err){}
$(el).attr(options.attribute,uri.href());},extract_qurl:function(el,options,uri){return this.extract(el,options,uri,'qurl');},extract_url:function(el,options,uri){return this.extract(el,options,uri,'url');},extract:function(el,options,uri,param){var datamap=uri.search(true);var quri=URI(URI.decode(datamap[param]));if(uri.fragment()!==''){if(quri.path()!=="undefined"){quri.fragment(uri.fragment());}}
return quri;},rewrite:function(el,options,uri){var target=URI({protocol:uri.protocol(),hostname:options.hostnames[0],path:options.loginpath});var qurl=this.extract_qurl(this.element,this.options,uri).href();if(qurl==="undefined"){qurl=this.extract_url(this.element,this.options,uri).href();}
if(qurl==="undefined"){$.error('Unable to extract proxy URL: ');}
target.addQuery({qurl:qurl});$(this.element).attr(this.options.attribute,target.href());},prefix:function(el,options,uri){var rewriteByHost=true;var rewriteByPath=false;var offset=uri.host().indexOf(options.hostnames[0]);if(offset>0){uri.host(uri.host().slice(0,offset-1));offset=uri.host().indexOf('.http.');if(offset>0){uri.host(uri.host().slice(offset+6));}
offset=uri.host().indexOf('-https-');if(offset>0){uri.host(uri.host().slice(offset+7));}
if(uri.protocol()==='https'&&uri.host().search(/\./)<1){uri.host(uri.host().replace(/-/g,'.'));uri.host(uri.host().replace(/\.\./,'-'));uri.host(uri.host().replace(/databases\.abc\.clio\.com/,'databases.abc-clio.com'));}}else{rewriteByHost=false;rewriteByPath=true;}
if(rewriteByPath===true){var segmentArray=uri.segment();for(var idx=0;idx<segmentArray.length;idx++){if(segmentArray[idx].startsWith('MuseProtocol')){uri.protocol(segmentArray[idx].split('=')[1]);}
if(segmentArray[idx].startsWith('MuseHost')){uri.host(segmentArray[idx].split('=')[1]);}
if(segmentArray[idx]==='MusePath'){uri.path(segmentArray.slice(idx+1).join('/'));}}}
var target=URI({protocol:uri.protocol(),hostname:options.hostnames[0],path:options.loginpath});target.addQuery({qurl:uri.href()});$(el).attr(options.attribute,target.href());}};$.fn[pluginName]=function(options){return this.each(function(){if(!$.data(this,"plugin_"+pluginName)){$.data(this,"plugin_"+pluginName,new Plugin(this,options));}});};})(jQuery,window,document);