How to use @automattic/load-script - 10 common examples

To help you get started, we’ve selected a few @automattic/load-script examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Automattic / wp-calypso / client / blocks / location-search / index.jsx View on Github external
componentDidMount() {
		if ( ! autocompleteService ) {
			autocompleteService = {}; // if multiple components are initialized on the page, we'd want the script to load only once
			loadScript(
				`//maps.googleapis.com/maps/api/js?key=${ config(
					'google_maps_and_places_api_key'
				) }&libraries=places`,
				function() {
					// eslint-disable-next-line no-undef
					autocompleteService = new google.maps.places.AutocompleteService();
				}
			);
		}
	}
github Automattic / wp-calypso / client / lib / analytics / index.js View on Github external
const randomIdLength = 18; // 18 * 4/3 = 24 (base64 encoded chars).
					_ui = createRandomId( randomIdLength );
					document.cookie = cookie.serialize( 'tk_ai', _ui );
				}
			}
		}

		blockedTracksDebug( 'Loading /nostats.js', { _ut, _ui } );
		return loadScript(
			'/nostats.js?_ut=' + encodeURIComponent( _ut ) + '&_ui=' + encodeURIComponent( _ui )
		);
	} );
}

if ( typeof document !== 'undefined' ) {
	_loadTracksResult = loadScript( '//stats.wp.com/w.js?60' );
}

function buildQuerystring( group, name ) {
	let uriComponent = '';

	if ( 'object' === typeof group ) {
		for ( const key in group ) {
			uriComponent += '&x_' + encodeURIComponent( key ) + '=' + encodeURIComponent( group[ key ] );
		}
	} else {
		uriComponent = '&x_' + encodeURIComponent( group ) + '=' + encodeURIComponent( name );
	}

	return uriComponent;
}
github Automattic / wp-calypso / client / lib / load-jquery-dependent-script-desktop-wrapper / index.js View on Github external
export function loadjQueryDependentScriptDesktopWrapper( url, callback ) {
	debug( `Loading a jQuery dependent script from "${ url }"` );

	// It is not possible to expose jQuery globally in Electron App: https://github.com/atom/electron/issues/254.
	// It needs to be loaded using require and npm package.
	if ( config.isEnabled( 'desktop' ) ) {
		debug( `Attaching jQuery from node_modules to window for "${ url }"` );
		asyncRequire( 'jquery', $ => {
			window.$ = window.jQuery = $;
			loadScript( url, callback );
		} );
		return;
	}

	loadjQueryDependentScript( url, callback );
}
github Automattic / wp-calypso / client / lib / directly / index.js View on Github external
return new Promise( ( resolve, reject ) => {
		loadScript( DIRECTLY_RTM_SCRIPT_URL, function( error ) {
			if ( error ) {
				return reject( new Error( `Failed to load script "${ error.src }".` ) );
			}
			resolve();
		} );
	} );
}
github Automattic / wp-calypso / client / components / social-buttons / facebook.js View on Github external
async loadDependency() {
		if ( ! window.FB ) {
			await loadScript( '//connect.facebook.net/en_US/sdk.js' );
		}

		return window.FB;
	}
github Automattic / wp-calypso / client / components / embed-container / index.jsx View on Github external
loader = new Promise( function( resolve, reject ) {
			loadScript( scriptUrl, function( err ) {
				if ( err ) {
					reject( err );
				} else {
					resolve();
				}
			} );
		} );
		loaders[ scriptUrl ] = loader;
github Automattic / wp-calypso / client / lib / payment-gateway-loader / index.js View on Github external
return new Promise( ( resolve, reject ) => {
		if ( window[ gatewayNamespace ] ) {
			resolve( window[ gatewayNamespace ] );
			return;
		}

		loadScript( gatewayUrl, function( error ) {
			if ( error ) {
				reject( error );
				return;
			}

			debug( 'Payment gateway ' + gatewayNamespace + ' loaded for the first time' );
			resolve( window[ gatewayNamespace ] );
		} );
	} );
};
github Automattic / wp-calypso / client / lib / siftscience / index.js View on Github external
recordUser: function() {
		if ( ! hasLoaded ) {
			window._sift.push( [ '_setAccount', config( 'siftscience_key' ) ] );
			window._sift.push( [ '_setUserId', user().get().ID ] );
			window._sift.push( [ '_trackPageview' ] );

			hasLoaded = true;
			loadScript( SIFTSCIENCE_URL, function( error ) {
				if ( error ) {
					debug( 'Error loading siftscience' );
				} else {
					debug( 'siftscience loaded successfully' );
				}
			} );
		}
	},
};
github Automattic / wp-calypso / client / components / social-buttons / google.js View on Github external
async loadDependency() {
		if ( ! window.gapi ) {
			await loadScript( 'https://apis.google.com/js/platform.js' );
		}

		return window.gapi;
	}

@automattic/load-script

Async Script Loader.

GPL-2.0
Latest version published 5 years ago

Package Health Score

73 / 100
Full package analysis