Created
January 10, 2014 12:10
-
-
Save cggaurav/8350907 to your computer and use it in GitHub Desktop.
Embed Inject Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WHAT? | |
// | |
// Script to embed Gendly anywhere | |
// | |
// USAGE: | |
// | |
// <script src="//js.gendly.com" async="true" defer="true"></script> | |
// | |
(function(window, document) { | |
var Gendly = {}; | |
Gendly.logger = window.console; | |
Gendly.injectScript = function (url, exports, attrs, callback) { | |
callback = callback || function () {}; | |
var head = document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
var done = false; | |
script.src = url; | |
script.setAttribute('class', ' -gendly -embed'); | |
script.setAttribute('type', 'text/javascript'); | |
for (var attr in attrs) { | |
script.setAttribute(attr, attrs[attr]); | |
} | |
var doneCallback = function () { | |
if (!doneCallback.called) { | |
doneCallback.called = true; | |
callback.apply(callback, arguments); | |
} | |
}; | |
script.onload = script.onreadystatechange = function(){ | |
if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') ) { | |
done = true; | |
var _exports = exports.map(function(key) { | |
var obj = window[key]; | |
Gendly[key] = obj; | |
return obj; | |
}); | |
callback.apply(callback, _exports); | |
script.onload = script.onreadystatechange = null; | |
head.removeChild(script); | |
} | |
}; | |
head.appendChild(script); | |
}; | |
Gendly.config = { | |
require: { | |
waitSeconds: 5, | |
paths: { | |
'require': 'http://js.gendly.com/require', | |
'jquery': 'http://js.gendly.com/jquery' | |
}, | |
map: { | |
'*': { | |
'jquery': 'jquery-private' | |
}, | |
'jquery-private': { 'jquery': 'jquery' } | |
}, | |
config: { | |
'text': { | |
useXhr: function (url, protocol, hostname, port) { | |
// return true if you want to allow this url, given that the | |
// text plugin thinks the request is coming from protocol, hostname, port. | |
return true; | |
} | |
} | |
} | |
} | |
}; | |
Gendly.inject = function (callback) { | |
callback = callback || function () {}; | |
Gendly.injectScript(Gendly.config.require.paths.require + '.js', ['require', 'define'], {async: true}, function ($require, $define) { | |
$require = $require || require; | |
$define = $define || define; | |
$define('jquery-private', ['jquery'], function (jquery) { return jquery.noConflict(true); }); | |
$require.config(Gendly.config.require); | |
$require(['jquery'], function($) { | |
Gendly.$ = $; | |
callback(); | |
}); | |
}); | |
}; | |
Gendly.inject(function(){ | |
var $ = Gendly.$; | |
$(document).ready(function($) { | |
var only_gendly_event_links = function(url) { | |
return /gendly\.com\/.{10}\/event/i.test(url); // skip non-photo image filenames/-paths (e.g. gifs, ads) | |
}; | |
$.each($('a'), function() { | |
var a = this; | |
var url = this.href; | |
var hostName = $('<a>').prop('href', url).prop('hostname'); | |
if(only_gendly_event_links(url)){ | |
$(a).addClass('gendly-link'); | |
} | |
}); | |
$('.gendly-link').on('click', function(e) { | |
e.preventDefault(); | |
// console.log("CLICKED", this); | |
var url = this.href; | |
Gendly.$ID = url.split('.com/')[1].split('/event')[0]; | |
// console.log('GENDLY_ID', Gendly.$ID); | |
Gendly.$iframeWrapper = $('<div id="gendly-iframe-wrapper" style="position:fixed;width:100%;top:70px;z-index:1346;text-align:center;"></div>'); | |
Gendly.$iframeBack = $('<div id="gendly-iframe-back" style="background:#000;opacity:0.8;width:100%;height:100%;top:0;left:0;position:fixed;z-index:1345;"></div') | |
Gendly.$iframe = $('<iframe rel="prerender" src="//gendly.com/' + Gendly.$ID + '/event" style="width:900px;height:450px;overflow:hidden;border:none;"></iframe>') | |
$('body').append(Gendly.$iframeBack); | |
$('body').append(Gendly.$iframeWrapper); | |
$(Gendly.$iframeWrapper).append(Gendly.$iframe); | |
$(Gendly.$iframeBack).on('click', function(e){ | |
Gendly.$iframe.hide(); | |
Gendly.$iframeWrapper.hide(); | |
Gendly.$iframeBack.hide(); | |
}); | |
}); | |
}); | |
}); | |
// Export Gendly to Window Object | |
window.Gendly = Gendly; | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment