background.js
/**
 * Handle webNavigation and inject content script if URL matches.
 * @param {Object} data - Contains properties about webNavigation destination
 */
Handle webNavigation and inject content script if URL matches.
@param {Object} data - Contains properties about webNavigation destination
Handle webNavigation and inject content script if URL matches . @param { Object } data - Contains properties about webNavigation destination
function handleNavigation(data) {
	if (!data.url) return
	const url = new URL(data.url)
	if (data.transitionType === 'auto_subframe') return
	function handleInjection(_data) {
		const { sites, time } = STORAGE_CACHE
		if (time.active) {
			const now = new Date()
			const h = now.getHours()
			const m = now.getMinutes()
			const hh_mm = `${h < 10 ? '0' + h : h}:${h < 10 ? '0' + m : m}`
			const isActive = time.from < hh_mm && time.to > hh_mm
			if (!isActive) return
		}
		if (Object.values(sites).some((e) => e === url.hostname)) {
			 We also have to inject the polyfill here since executeScript doesn't support modules (yet)
			chrome.scripting.executeScript(
				{
					target: { tabId: _data.tabId },
					files: ['js/polyfills/custom-elements.min.js']
				},
				() => {
					chrome.scripting.executeScript({
						target: { tabId: _data.tabId },
						files: ['js/inject.js']
					})
				}
			)
			chrome.scripting.insertCSS({
				target: { tabId: _data.tabId },
				files: ['style/inject.css']
			})
		}
	}
	if (!STORAGE_CACHE) {
		chrome.storage.local.get(undefined, (storage) => {
			STORAGE_CACHE = storage
			handleInjection(data)
		})
	} else {
		handleInjection(data)
	}
}
function handleNavigation ( data ) { if ( ! data . url ) return const url = new URL ( data . url ) if ( data . transitionType === 'auto_subframe' ) return function handleInjection ( _data ) { const { sites , time } = STORAGE_CACHE if ( time . active ) { const now = new Date ( ) const h = now . getHours ( ) const m = now . getMinutes ( ) const hh_mm = ` ${ h < 10 ? '0' + h : h } ${ h < 10 ? '0' + m : m } ` const isActive = time . from < hh_mm && time . to > hh_mm if ( ! isActive ) return } if ( Object . values ( sites ) . some ( ( e ) => e === url . hostname ) ) { chrome . scripting . executeScript ( { target : { tabId : _data . tabId } , files : [ 'js/polyfills/custom-elements.min.js' ] } , ( ) => { chrome . scripting . executeScript ( { target : { tabId : _data . tabId } , files : [ 'js/inject.js' ] } ) } ) chrome . scripting . insertCSS ( { target : { tabId : _data . tabId } , files : [ 'style/inject.css' ] } ) } } if ( ! STORAGE_CACHE ) { chrome . storage . local . get ( undefined , ( storage ) => { STORAGE_CACHE = storage handleInjection ( data ) } ) } else { handleInjection ( data ) } }
Handle webNavigation and inject content script if URL matches.
Handle webNavigation and inject content script if URL matches .
// Prevent extension triggering on embedded content // Compare strings using lexicographic order // to allow timeframes between two days (ex: 23:00 to 02:00) // Inject WebComponents polyfill since Webcomponents are // not supported (yet), see: https://bugs.chromium.org/p/chromium/issues/detail?id=390807#c59 // // We also have to inject the polyfill here since executeScript doesn't support modules (yet)