src/index.js
/**
 * Initialize.
 *
 * @param {string}   contentSelector         - CSS Selector to find the elements for speaking.
 * @param {Element}  rootElement             - Root element within which to look for content
 * @param {Object}   chunkifyOptions         - Options passed into chunkify.
 * @param {boolean}  useDashicons            - Whether to use Dashicons.
 * @param {Object}   defaultUtteranceOptions - Default utterance options when none are supplied from localStorage.
 * @param {Function} hasSystemSupport        - Function which determines whether the user's operating supports the functionality. Defaults to return false on iOS or Android.
 * @returns {Promise} Promise.
 */
Initialize.
@param {string}   contentSelector         - CSS Selector to find the elements for speaking.
@param {Element}  rootElement             - Root element within which to look for content
@param {Object}   chunkifyOptions         - Options passed into chunkify.
@param {boolean}  useDashicons            - Whether to use Dashicons.
@param {Object}   defaultUtteranceOptions - Default utterance options when none are supplied from localStorage.
@param {Function} hasSystemSupport        - Function which determines whether the user's operating supports the functionality. Defaults to return false on iOS or Android.
@returns {Promise} Promise.
Initialize . @param { string } contentSelector - CSS Selector to find the elements for speaking . @param { Element } rootElement - Root element within which to look for content @param { Object } chunkifyOptions - Options passed into chunkify . @param { boolean } useDashicons - Whether to use Dashicons . @param { Object } defaultUtteranceOptions - Default utterance options when none are supplied from localStorage . @param { Function } hasSystemSupport - Function which determines whether the user ' s operating supports the functionality . Defaults to return false on iOS or Android . @returns { Promise } Promise .
function initialize( {
	rootElement,
	contentSelector = CONTENT_SELECTOR,
	useDashicons,
	chunkifyOptions,
	defaultUtteranceOptions = DEFAULT_UTTERANCE_OPTIONS,
	hasSystemSupport = HAS_SYSTEM_SUPPORT,
} = {} ) {
	customDefaultUtteranceOptions = defaultUtteranceOptions;
	return new Promise( ( resolve, reject ) => {
		if ( typeof speechSynthesis === 'undefined' || typeof SpeechSynthesisUtterance === 'undefined' ) {
			reject( 'speech_synthesis_not_supported' );
			return;
		}
		if ( ! hasSystemSupport() ) {
			reject( 'system_not_supported' );
			return;
		}
		const mutationObserver = new MutationObserver( ( mutations ) => {
			for ( const mutation of mutations ) {
				for ( const addedNode of [ ...mutation.addedNodes ].filter( ( node ) => node.nodeType === Node.ELEMENT_NODE ) ) {
					createSpeeches( {
						element: addedNode,
						contentSelector,
						useDashicons,
						chunkifyOptions,
					} );
				}
				for ( const removedNode of [ ...mutation.removedNodes ].filter( ( node ) => node.nodeType === Node.ELEMENT_NODE ) ) {
					destroySpeeches( {
						element: removedNode,
						contentSelector,
					} );
				}
			}
		} );
		const uponReady = () => {
			const element = rootElement || document.body;
			window.addEventListener( 'unload', () => {
				speechSynthesis.cancel();
			} );
			createSpeeches( {
				element,
				contentSelector,
				chunkifyOptions,
				useDashicons,
			} );
			mutationObserver.observe( element, {
				childList: true,
				subtree: true,
			} );
			resolve();
		};
		if ( 'complete' === document.readyState ) {
			uponReady();
		} else {
			document.addEventListener( 'DOMContentLoaded', uponReady );
		}
	} );
}
function initialize ( { rootElement , contentSelector = CONTENT_SELECTOR , useDashicons , chunkifyOptions , defaultUtteranceOptions = DEFAULT_UTTERANCE_OPTIONS , hasSystemSupport = HAS_SYSTEM_SUPPORT , } = { } ) { customDefaultUtteranceOptions = defaultUtteranceOptions ; return new Promise ( ( resolve , reject ) => { if ( typeof speechSynthesis === 'undefined' || typeof SpeechSynthesisUtterance === 'undefined' ) { reject ( 'speech_synthesis_not_supported' ) ; return ; } if ( ! hasSystemSupport ( ) ) { reject ( 'system_not_supported' ) ; return ; } const mutationObserver = new MutationObserver ( ( mutations ) => { for ( const mutation of mutations ) { for ( const addedNode of [ ... mutation . addedNodes ] . filter ( ( node ) => node . nodeType === Node . ELEMENT_NODE ) ) { createSpeeches ( { element : addedNode , contentSelector , useDashicons , chunkifyOptions , } ) ; } for ( const removedNode of [ ... mutation . removedNodes ] . filter ( ( node ) => node . nodeType === Node . ELEMENT_NODE ) ) { destroySpeeches ( { element : removedNode , contentSelector , } ) ; } } } ) ; const uponReady = ( ) => { const element = rootElement || document . body ; window . addEventListener ( 'unload' , ( ) => { speechSynthesis . cancel ( ) ; } ) ; createSpeeches ( { element , contentSelector , chunkifyOptions , useDashicons , } ) ; mutationObserver . observe ( element , { childList : true , subtree : true , } ) ; resolve ( ) ; } ; if ( 'complete' === document . readyState ) { uponReady ( ) ; } else { document . addEventListener ( 'DOMContentLoaded' , uponReady ) ; } } ) ; }
Initialize.
Initialize .
// Probably a bug in Chrome that utterance is not canceled upon unload.