Skip to content

Instantly share code, notes, and snippets.

@rviscomi
Last active April 3, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rviscomi/700f8cc86490f364843f3feaaecbff89 to your computer and use it in GitHub Desktop.
Save rviscomi/700f8cc86490f364843f3feaaecbff89 to your computer and use it in GitHub Desktop.
WPT custom metrics script for third party libs
[lib-version]
return '2.8.2';
[3pl]
var thirdParties = [];
function addThirdParty(name, version) {
if (!name) {
return;
}
name = name.replace('@', '\@');
version = version && version.replace('@', '\@');
name += '@' + (version || 'null');
thirdParties.push(name);
}
var d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests = { 'GWT': { test: function(win) { var doc = win.document, hasHistFrame = doc.getElementById('__gwt_historyFrame'), hasGwtUid = doc.gwt_uid, hasBodyListener = doc.body.__listener, hasBodyEventBits = doc.body.__eventBits, hasModules = win.__gwt_activeModules, hasJsonP = win.__gwt_jsonp__, hasRootWinApp = win.__gwt_scriptsLoaded || win.__gwt_stylesLoaded || win.__gwt_activeModules; if(hasHistFrame || hasGwtUid || hasBodyListener || hasBodyEventBits || hasModules || hasJsonP || hasRootWinApp) { var frames = doc.getElementsByTagName('iframe'), gwtVersion = 'unknown'; for(var n=0; n<frames.length; n++) { try { var hasNegativeTabIndex = frames[n].tabIndex < 0; if(hasNegativeTabIndex && frames[n].contentWindow && frames[n].contentWindow.$gwt_version) { gwtVersion = frames[n].contentWindow.$gwt_version; break; } } catch(e) {} } if(gwtVersion=='0.0.999') { gwtVersion = 'Google Internal'; } return { version: gwtVersion }; } return false; } }, 'Ink': { test: function(win) { if (win.Ink && win.Ink.createModule) { return { version: 'unknown' }; } return false; } }, 'Vaadin': { test: function(win) { if (win.vaadin && win.vaadin.registerWidgetset) { return { version: 'unknown' }; } return false; } }, 'Bootstrap': { test: function(win) { var jQueryAvailable = win.$ && win.$.fn, RE_PREFIX_V2 = '\\$this\\.data\\((?:\'|")', RE_PREFIX_V3 = '\\$this\\.data\\((?:\'|")(?:bs\\.){1}', bootstrapComponents = [ 'affix', 'alert', 'button', 'carousel', 'collapse', 'dropdown', 'modal', 'popover', 'scrollspy', 'tab', 'tooltip' ]; if(jQueryAvailable) { var bootstrapVersion; bootstrapComponents.some(function(component) { if(win.$.fn[component]) { if(win.$.fn[component].Constructor && win.$.fn[component].Constructor.VERSION) { bootstrapVersion = win.$.fn[component].Constructor.VERSION; return true; } else if(new RegExp(RE_PREFIX_V3 + component).test(win.$.fn[component].toString())) { bootstrapVersion = '>= 3.0.0 & <= 3.1.1'; return true; } else if(new RegExp(RE_PREFIX_V2 + component).test(win.$.fn[component].toString())) { bootstrapVersion = '>= 2.0.0 & <= 2.3.2'; return true; } } return false; }); if (bootstrapVersion) { return { version: bootstrapVersion }; } } return false; } }, 'Zurb': { test: function(win) { if(win.Foundation && win.Foundation.version) { return { version: win.Foundation.version }; } return false; } }, 'Polymer': { test: function(win) { if(win.Polymer && win.Polymer.version) { return { version: win.Polymer.version }; } return false; } }, 'Highcharts': { test: function(win) { if(win.Highcharts && win.Highcharts.version) { return { version: win.Highcharts.version }; } return false; } }, 'InfoVis': { test: function(win) { if(win.$jit && win.$jit.version) { return { version: win.$jit.version }; } return false; } }, 'FlotCharts': { test: function(win) { if(win.$ && win.$.plot && win.$.plot.version) { return { version: win.$.plot.version }; } return false; } }, 'Blackbird': { test: function(win) { if(win.log && win.log.warn) { return { version: "N/A"}; } return false; } }, 'CreateJS': { test: function(win) { if(win.Stage || win.Shape || win.Container) { return { version: "N/A"}; } return false; } }, 'Google Maps': { test: function(win) { if (win.google && win.google.maps && win.google.maps.version) { return { version: win.google.maps.version }; } return false; } }, 'jQuery': { test: function(win) { var jq = win.jQuery || win.$ || win.$jq || win.$j; if(jq && jq.fn && jq.fn.jquery) { return { version: jq.fn.jquery }; } return false; } }, 'jQuery UI': { test: function(win) { var jq = win.jQuery || win.$ || win.$jq || win.$j; if(jq && jq.fn && jq.fn.jquery && jq.ui) { var plugins = 'accordion,datepicker,dialog,draggable,droppable,progressbar,resizable,selectable,slider,menu,grid,tabs'.split(','), concat = []; for (var i=0; i < plugins.length; i++) { if(jq.ui[plugins[i]]) concat.push(plugins[i].substr(0,1).toUpperCase() + plugins[i].substr(1)); } return { version: jq.ui.version, details: concat.length ? 'Plugins used: '+concat.join(',') : '' }; } return false; } }, 'Dojo': { test: function(win) { if(win.dojo && win.dojo.version) { return { version: win.dojo.version.toString(), details: 'Details: '+(win.dijit ? 'Uses Dijit' : 'none') }; } return false; } }, 'Prototype': { test: function(win) { if(win.Prototype && win.Prototype.Version) { return { version: win.Prototype.Version }; } return false; } }, 'Scriptaculous': { test: function(win) { if(win.Scriptaculous && win.Scriptaculous.Version) { return { version: win.Scriptaculous.Version }; } return false; } }, 'MooTools': { test: function(win) { if(win.MooTools && win.MooTools.version) { return { version: win.MooTools.version }; } return false; } }, 'Spry': { test: function(win) { if (win.Spry && win.Spry.Data) { return { version: '' }; } return false; } }, 'YUI 2': { test: function(win) { if (win.YAHOO && win.YAHOO.VERSION) { return { version: win.YAHOO.VERSION }; } return false; } }, 'YUI 3': { test: function(win) { if (win.YUI && win.YUI.Env && win.YUI.version) { return { version: win.YUI.version }; } return false; } }, 'Qooxdoo': { test: function(win) { if(win.qx && win.qx.Bootstrap) { return { version: '' }; } return false; } }, 'Ext JS': { test: function(win) { if(win.Ext && win.Ext.version) { return { version: win.Ext.version }; } else if (win.Ext && win.Ext.versions) { return { version: win.Ext.versions.core.version }; } return false; } }, 'base2': { test: function(win) { if(win.base2 && win.base2.version) { return { version: win.base2.version }; } return false; } }, 'Closure Library': { test: function(win) { if(win.goog && win.goog.provide) { return { version: '' }; } return false; } }, 'Rapha&euml;l': { test: function(win) { if (win.Raphael && win.Raphael.circle) { return { version: win.Raphael.version }; } return false; } }, 'React': { test: function(win) { if (win.React && win.React.createClass) { return { version: win.React.version }; } return false; } }, 'Modernizr': { test: function(win) { if (win.Modernizr && win.Modernizr.addTest) { return { version: win.Modernizr._version }; } return false; } }, 'Processing.js': { test: function(win) { if(win.Processing && win.Processing.box) { return { version: Processing.version }; } return false; } }, 'Backbone': { test: function(win) { if (win.Backbone && win.Backbone.Model.extend) { return {version: win.Backbone.VERSION}; } return false; } }, 'Leaflet': { test: function(win) { if (win.L && win.L.GeoJSON && (win.L.marker || win.L.Marker)) { return { version: win.L.version || win.L.VERSION || null }; } return false; } }, 'Mapbox': { test: function(win) { if (win.L && win.L.mapbox && win.L.mapbox.geocoder) { return { version: win.L.mapbox.VERSION }; } return false; } }, 'Lo-Dash': { test: function(win) { var _ = typeof (_ = win._) == 'function' && _, chain = typeof (chain = _ && _.chain) == 'function' && chain, wrapper = (chain || _ || function() { return {}; })(1); if (_ && _.VERSION && wrapper.__wrapped__) { return { version: _.VERSION }; } return false; } }, 'Underscore': { test: function(win) { if (win._ && win._.VERSION && typeof win._.tap === 'function' && !d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests['Lo-Dash'].test(win)) { return {version: win._.VERSION}; } return false; } }, 'Sammy': { test: function(win) { if (win.Sammy && win.Sammy.VERSION && win.Sammy.Application.curry) { return {version: win.Sammy.VERSION}; } return false; } }, 'Rico': { test: function(win) { if (win.Rico && win.Rico.Version) { return {version: win.Rico.Version}; } return false; } }, 'MochiKit': { test: function(win) { if (win.MochiKit && win.MochiKit.Base.module) { return {version: MochiKit.VERSION}; } return false; } }, 'gRapha&euml;l': { test: function(win) { if (win.Raphael && win.Raphael.fn.g) { return {version: ''}; } return false; } }, 'Glow': { test: function(win) { if (win.gloader) { return {version: ''}; } else if (win.glow && win.glow.dom) { return {version: win.glow.VERSION}; } else if (win.Glow) { return {version: win.Glow.version}; } return false; } }, 'Socket.IO': { test: function(win) { if (win.io && (win.io.sockets || win.io.Socket) && win.io.version) { return {version: win.io.version}; } return false; } }, 'Mustache': { test: function(win) { if (win.Mustache && win.Mustache.to_html) { return {version: win.Mustache.version}; } return false; } }, 'Fabric.js': { test: function(win) { if (win.fabric && win.fabric.util) { return {version: win.fabric.version}; } return false; } }, 'FuseJS': { test: function(win) { if (win.fuse && win.fuse.version) { return {version: win.fuse.version}; } return false; } }, 'Tween.js': { test: function(win) { if (win.TWEEN && win.TWEEN.Easing) { return {version: ''}; } return false; } }, 'SproutCore': { test: function(win) { if (win.SC && win.SC.Application) { return {version: ''}; } return false; } }, 'Zepto.js': { test: function(win) { if (win.Zepto && win.Zepto.fn) { return {version: ''}; } return false; } }, 'three.js': { test: function(win) { if (win.THREE && win.THREE.REVISION) { return {version: 'r' + win.THREE.REVISION}; } else if (win.THREE) { return {version: ''}; } return false; } }, 'PhiloGL': { test: function(win) { if (win.PhiloGL && win.PhiloGL.Camera) { return {version: win.PhiloGL.version}; } return false; } }, 'CamanJS': { test: function(win) { if (win.Caman && win.Caman.version) { return {version: win.Caman.version.release}; } return false; } }, 'yepnope': { test: function(win) { if (win.yepnope) { return {version: ''}; } return false; } }, 'LABjs': { test: function(win) { if (win.$LAB) { return {version: ''}; } return false; } }, 'Head JS': { test: function(win) { if (win.head && win.head.js) { return {version: ''}; } return false; } }, 'ControlJS': { test: function(win) { if (win.CJS && win.CJS.start) { return {version: ''}; } return false; } }, 'RequireJS': { test: function(win) { var req = win.require || win.requirejs; if (req && (req.load || (req.s && req.s.contexts && req.s.contexts._ && (req.s.contexts._.loaded || req.s.contexts._.load)))) { return { version: req.version || null }; } return false; } }, 'RightJS': { test: function(win) { if (win.RightJS && win.RightJS.isNode) { return {version: win.RightJS.version}; } return false; } }, 'jQuery Tools': { test: function(win) { var jq = win.jQuery || win.$; if(jq && jq.tools) { return { version: jq.tools.version }; } return false; } }, 'Pusher': { test: function(win) { if(win.Pusher && win.Pusher.Channel) { return { version: win.Pusher.VERSION }; } return false; } }, 'Paper.js': { test: function(win) { if(win.paper && win.paper.Point) { return { version: win.paper.version }; } return false; } }, 'Swiffy': { test: function(win) { if(win.swiffy) { return { version: '' }; } return false; } }, 'Move': { test: function(win) { if(win.move && win.move.compile) { return { version: win.move.version() }; } return false; } }, 'AmplifyJS': { test: function(win) { if(win.amplify && win.amplify.publish) { return { version: '' }; } return false; } }, 'Popcorn.js': { test: function(win) { if (win.Popcorn && win.Popcorn.Events) { return { version: win.Popcorn.version }; } return false; } }, 'D3': { test: function(win) { if (win.d3 && win.d3.select) { return { version: win.d3.version }; } return false; } }, 'Handlebars': { test: function(win) { if(win.Handlebars && win.Handlebars.compile) { return { version: win.Handlebars.VERSION }; } return false; } }, 'Knockout': { test: function(win) { if (win.ko && win.ko.applyBindings) { return { version: win.ko.version }; } return false; } }, 'Spine': { test: function(win) { if (win.Spine && win.Spine.Controller) { return {version: win.Spine.version}; } return false; } }, 'jQuery Mobile': { test: function(win) { var jq = win.jQuery || win.$ || win.$jq || win.$j; if(jq && jq.fn && jq.fn.jquery && jq.mobile) { return { version: jq.mobile.version || '' }; } return false; } }, 'WebFont Loader': { test: function(win) { if(win.WebFont && win.WebFont.load) { return { version: "N/A" }; } return false; } }, 'Angular': { test: function(win) { var ng = win.document.querySelector('[ng-version]'); if (ng && ng.getAttribute('ng-version')) { return { version: ng.getAttribute('ng-version') }; } return false; } }, 'AngularJS': { test: function(win) { var ng = win.angular; if(ng && ng.version && ng.version.full) { return { version: ng.version.full }; } return false; } }, 'Ember.js': { test: function(win) { var ember = win.Ember || win.Em; if (ember && ember.VERSION) { return { version: ember.VERSION }; } return false; } }, 'Hammer.js': { test: function(win) { var hammer = win.Hammer; if(hammer && hammer.Pinch) { return { version: hammer.VERSION || "&lt; 1.0.10" }; } return false; } }, 'Visibility.js': { test: function(win) { var visibility = win.Visibility; if(visibility && visibility.every) { return { version: 'unknown' }; } return false; } }, 'Velocity.js': { test: function(win) { var jq = win.jQuery || win.$, velocity = jq ? jq.Velocity : win.Velocity; if(velocity && velocity.RegisterEffect) { return { version: velocity.version.major + "." + velocity.version.minor + "." + velocity.version.patch }; } return false; } }, 'IfVisible.js': { test: function(win) { var iv = win.ifvisible; if(iv && iv.__ceGUID === "ifvisible.object.event.identifier") { return { version: "N/A" }; } return false; } }, 'Pixi.js': { test: function(win) { var px = win.PIXI; if(px && px.WebGLRenderer && px.VERSION) { return { version: px.VERSION.replace('v', '') || null }; } return false; } }, 'DC.js': { test: function(win) { var dc = win.dc; if(dc && dc.version && dc.registerChart) { return { version: dc.version }; } return false; } }, 'Greensock JS': { test: function(win) { var gs = win.TweenMax || win.TweenLite ; if(gs && gs.version) { return { version: gs.version } } return false; } }, 'FastClick': { test: function(win) { if(win.FastClick && win.FastClick.notNeeded) { return { version: 'N/A' } } return false; } }, 'Isotope': { test: function(win) { var iso = win.Isotope || (win.$ != null && win.$.Isotope); if(iso) { return { version: 'N/A' } } return false; } }, 'Marionette': { test: function(win) { if(win.Marionette && win.Marionette.Application) { return { version: win.Marionette.VERSION }; } return false; } }, 'Can': { test: function (win) { if (win.can && win.can.Construct) { return { version: win.can.VERSION }; } return false; } }, 'Vue': { test: function(win) { if (win.Vue && win.Vue.nextTick) { return { version: win.Vue.version || 'N/A' }; } return false; } }, 'Two': { test: function(win) { if (win.Two && win.Two.Utils) { return { version: win.Two.Version }; } return false; } }, 'Brewser': { test: function(win) { if(win.BREWSER && win.BREWSER.ua) { return { version: BREWSER.VERSION }; } return false; } }, 'Material Design Lite': { test: function(win) { if(win.componentHandler && win.componentHandler.upgradeElement) { return { version: 'N/A'}; } return false; } }, 'Kendo UI': { test: function(win) { if (win.kendo && win.kendo.View && win.kendo.View.extend) { return {version: win.kendo.version}; } return false; } }, 'Matter.js': { test: function(win) { if (win.Matter && win.Matter.Engine) { return {version: 'N/A'}; } return false; } }, 'Riot': { test: function(win) { if (win.riot && win.riot.mixin) { return { version: win.riot.version }; } return false; } }, 'Sea.js': { test: function(win) { if(win.seajs && win.seajs.use) { return { version: win.seajs.version }; } return false; } }, 'Moment.js': { test: function(win) { if(win.moment && (win.moment.isMoment || win.moment.lang)) { return { version: win.moment.version || null }; } return false; } }, 'Moment Timezone': { test: function(win) { if (win.moment && win.moment.tz) { return { version: win.moment.tz.version || null }; } return false; } }, 'ScrollMagic': { test: function(win) { if (win.ScrollMagic && win.ScrollMagic.Controller) { return {version: ScrollMagic.version}; } return false; } }, 'SWFObject': { test: function(win) { if (win.swfobject && win.swfobject.embedSWF) { return { version: win.swfobject.version || null }; } else if(win.deconcept && win.deconcept.SWFObject) { return { version: null }; } return false; } }, 'FlexSlider': { test: function(win) { var jq = win.jQuery || win.$ || win.$jq || win.$j; if (jq && jq.fn && jq.fn.jquery && jq.flexslider){ return { version: null }; } return false; } }, 'SPF': { test: function(win) { if (win.spf && win.spf.init) { return { version: null }; } return false; } }, 'Numeral.js': { test: function(win) { if (win.numeral && win.isNumeral) { return { version: win.numeral.version || null }; } return false; } } };
Object.entries(d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests).forEach(([name, lib]) => {
const result = lib.test(window);
if (result) {
addThirdParty(name, result.version);
}
});
return JSON.stringify(thirdParties);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment