diff --git a/README.md b/README.md index bbb9123..eb0b364 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ $ npm install safelink-jquery ## Or use CDN Directly ```html - + // or - + ``` ## Usage diff --git a/package.json b/package.json index 35a345d..c3cf638 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "safelink-jquery", - "version": "1.0.0", + "version": "1.1.0", "description": "Safelink jQuery Plugin", "main": "src/safelink.js", "directories": { diff --git a/src/safelink.js b/src/safelink.js index e116140..e96cf21 100644 --- a/src/safelink.js +++ b/src/safelink.js @@ -1,7 +1,7 @@ /** * Safelink jQuery plugin * @author M ABD AZIZ ALFIAN - * @version 1.0.0 + * @version 1.1.0 * @repository https://github.com/aalfiann/safelink-jquery */ (function($){ @@ -9,14 +9,24 @@ $.fn.Safelink = function(options) { var config = { + encoded:'', + paramName: 'safelink', + timer: false, + timer_first: 8, + timer_second: 8, + firstTimerText: 'Loading safelink:', + secondTimerText: 'Scanning the link:', firstButtonText: 'Click Here !!!', secondButtonText: 'Get Link !!!' }; if(options) config = $.extend({},config,options); + var t1 = config.timer_first; + var t2 = config.timer_second; + var _crypto={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(a){var b="",d,e,f,g,h,j,k,l=0;for(a=_crypto._utf8_encode(a);l>2,h=(3&d)<<4|e>>4,j=(15&e)<<2|f>>6,k=63&f,isNaN(e)?j=k=64:isNaN(f)&&(k=64),b=b+this._keyStr.charAt(g)+this._keyStr.charAt(h)+this._keyStr.charAt(j)+this._keyStr.charAt(k);return b},decode:function(a){var d,e,f,g,h,j,k,b="",l=0;for(a=a.replace(/[^A-Za-z0-9\+\/\=]/g,"");l>4,e=(15&h)<<4|j>>2,f=(3&j)<<6|k,b+=String.fromCharCode(d),64!=j&&(b+=String.fromCharCode(e)),64!=k&&(b+=String.fromCharCode(f));return b=_crypto._utf8_decode(b),b},_utf8_encode:function(a){a=a.replace(/\r\n/g,"\n");for(var e,b="",d=0;de?b+=String.fromCharCode(e):127e?(b+=String.fromCharCode(192|e>>6),b+=String.fromCharCode(128|63&e)):(b+=String.fromCharCode(224|e>>12),b+=String.fromCharCode(128|63&e>>6),b+=String.fromCharCode(128|63&e));return b},_utf8_decode:function(a){for(var b="",d=0,e=c1=c2=0;de?(b+=String.fromCharCode(e),d++):191e?(c2=a.charCodeAt(d+1),b+=String.fromCharCode((31&e)<<6|63&c2),d+=2):(c2=a.charCodeAt(d+1),c3=a.charCodeAt(d+2),b+=String.fromCharCode((15&e)<<12|(63&c2)<<6|63&c3),d+=3);return b}}; - + this.Crypto = _crypto; this.encode = function(value) { @@ -29,20 +39,68 @@ this.build = function(element,btnClass) { btnClass = (!$.trim(btnClass)) ? '' : btnClass; - var url = new URL(window.location.href); - var link = url.searchParams.get("safelink"); - if(link) { - var decoded = this.decode(link); - $(this).append(''); - $(element).append(''); - $('#safelinkStart').on('click', function() { - $("#safelinkEnd").css('display', 'block').focus(); - }); + if($.trim(config.encoded)) { + this._process(element,config.encoded,btnClass); + } else { + function parse_query_string(e){for(var o=e.replace("?","").split("&"),n={},t=0;t 0){ + setTimeout(countDown1,1000); + } + if(t1==0){ + that.html(''); + } else { + that.html(config.firstTimerText+' '+t1); + } + } + function countDown2(){ + t2--; + if(t2 > 0){ + setTimeout(countDown2,1000); + } + if(t2==0){ + that_el.html(''); + } else { + that_el.html(config.secondTimerText+' '+t2); + } + } + setTimeout(countDown1,1000); + setTimeout(function() { + that.append(''); + $('#_safelinkStart').on('click', function() { + that.html(''); + that_el.attr('tabindex','-1').css('outline','none').focus(); + setTimeout(countDown2,1000); + setTimeout(function() { + that_el.append(''); + },((config.timer_second + 2)*1000)); + }); + },((config.timer_first + 2)*1000)); + } else { + $(this).append(''); + $('#_safelinkStart').on('click', function() { + $(this).html(''); + $(element).attr('tabindex','-1').css('outline','none').focus(); + $(element).append(''); + }); + } + } + return this; + }; })(jQuery); \ No newline at end of file diff --git a/test/safelink-jquery.js b/test/safelink-jquery.js index aedcf07..144f1a2 100644 --- a/test/safelink-jquery.js +++ b/test/safelink-jquery.js @@ -19,4 +19,49 @@ describe('jQuery safelink test', function() { assert.equal($().Safelink().decode('YWJjZGU='),'abcde'); }); + it('config value', function() { + $().Safelink({ + encoded:'123', + firstButtonText: 'Click here bro!', + secondButtonText: 'Continue to Link!' + }); + }); + + it('build will throws error if not in browser', function() { + assert.throws(function(){ + $('#safelinker1').Safelink({ + firstButtonText: 'Click here bro!', + secondButtonText: 'Continue to Link!' + }).build('#safelinker2','btn'); + },Error); + }); + + it('build with direct encoded', function() { + $('#safelinker1').Safelink({ + encoded:'123', + firstButtonText: 'Click here bro!', + secondButtonText: 'Continue to Link!' + }).build('#safelinker2','btn'); + }); + + it('build with timer', function() { + $('#safelinker1').Safelink({ + encoded:'123', + timer: true, + firstButtonText: 'Click here bro!', + secondButtonText: 'Continue to Link!' + }).build('#safelinker2'); + }); + + it('build with custom timer', function() { + $('#safelinker1').Safelink({ + encoded:'123', + timer: true, + timer_first:1, + timer_second:1, + firstButtonText: 'Click here bro!', + secondButtonText: 'Continue to Link!' + }).build('#safelinker2'); + }); + }); \ No newline at end of file