Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691a9edc-b5b8-800a-99a3-c32d9abccf68
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== User: oh but you see, there is much more. === oh but you see, there is much more. /** * A class for working with service workers * @class */ Q.ServiceWorker = { start: function(callback, options) { options = options || {}; if (!('serviceWorker' in navigator)) { Q.handle(callback, null, [false]); Q.ServiceWorker.onActive.handle(false); return console.warn('Q.ServiceWorker.start: Service workers are not supported.'); } var src = Q.info.serviceWorkerUrl; if (!src) { return callback(true); } Q.ServiceWorker.started = true; navigator.serviceWorker.getRegistration(src) .then(function (registration) { if (registration && registration.active && (!Q.info || !Q.info.updateServiceWorker)) { // our latest worker is already active var worker = registration.active; Q.handle(callback, Q.ServiceWorker, [worker, registration]); Q.handle(Q.ServiceWorker.onActive, Q.ServiceWorker, [worker, registration]); return; } navigator.serviceWorker.register(src) .then(function (registration) { log("Q.ServiceWorker.register", registration); if (options.update) { registration.update(); } registration.removeEventListener("updatefound", _onUpdateFound); registration.addEventListener("updatefound", _onUpdateFound); var worker; if (registration.active) { worker = registration.active; } else if (registration.waiting) { worker = registration.waiting; } else if (registration.installing) { worker = registration.installing; } if (worker) { Q.handle(callback, Q.ServiceWorker, [worker, registration]); Q.handle(Q.ServiceWorker.onActive, Q.ServiceWorker, [worker, registration]); } }).catch(function (error) { callback(error); console.warn("Q.ServiceWorker.start error", error); }); }); // Listen for cookie updates coming from the Service Worker navigator.serviceWorker.addEventListener('message', event => { const data = event.data || {}; if (data.type === 'Q.cookie' && data.cookies) { for (const [k, v] of Object.entries(data.cookies)) { // Update document.cookie so future page requests carry it document.cookie = encodeURIComponent(k) + '=' + encodeURIComponent(v) + '; path=/'; console.log('[SW] Updated cookie from service worker:', k); } } }); } } try { Q.ServiceWorker.isSupported = !!navigator.serviceWorker; } catch (e) { Q.ServiceWorker.isSupported = false; } Q.ServiceWorker.onActive = new Q.Event(); Q.ServiceWorker.onUpdateFound = new Q.Event(); function _onUpdateFound(event) { Q.handle(Q.ServiceWorker.onUpdateFound, Q.ServiceWorker, [event.target, event]); } function _startCachingWithServiceWorker() { if (!Q.ServiceWorker.isSupported || Q.ServiceWorker.skipCaching) { return false; } Q.ServiceWorker.start(function (worker, registration) { var items = []; var scripts = document.querySelectorAll("script[data-src]"); var styles = document.querySelectorAll("style[data-href]"); [scripts, styles].forEach(function (arr) { arr.forEach(function (element) { var content = element.innerText; var pathPrefix = element.getAttribute('data-path-prefix'); if (pathPrefix) { var prefixes = ['@import ', '@import "', "@import '", 'url(', 'url("', "url('"]; prefixes.forEach(function (prefix) { content = content.split(prefix + pathPrefix).join(prefix); }); } items.push({ url: element.getAttribute('data-src') || element.getAttribute('data-href'), content: content, headers: { 'Content-Type': element.getAttribute('type') } }); }); }); if (items.length) { worker.postMessage({ type: 'Q.Cache.put', items: items }) } }, { update: true }) } /** * Returns a <style> tag with the content of all the stylesheets included inline * @method stylesheetsInline * @static * @param {string} [$slotName=null] If provided, returns only the stylesheets added while filling this slot. * @return {string} the style tags and their contents inline */ static function stylesheetsInline ($slotName = null, $setLoaded = false) { if (empty(self::$stylesheets)) { return ''; } $baseUrl = Q_Request::baseUrl(); $dest = parse_url(Q_Request::url(), PHP_URL_PATH); $sheets = self::stylesheetsArray($slotName, false); $sheets_for_slots = $imported_for_slots = $relativePathPrefixes_for_slots = array(); $loaded = array(); if (!empty($sheets)) { foreach ($sheets as $stylesheet) { $href = ''; $media = 'screen,print'; $type = 'text/css'; extract($stylesheet, EXTR_IF_EXISTS); $ob = new Q_OutputBuffer(); if (Q_Valid::url($href) and !Q::startsWith(Q_Html::themedUrl($href), $baseUrl)) { $imported_for_slots[$stylesheet['slot']][$href] = "@import url($href);"; } else { list ($href, $filename) = Q_Html::themedUrlFilenameAndHash($href); if (!empty($loaded[$href])) { continue; } $stylesheetHref = Q_Html::themedUrl($stylesheet['href'], array('ignoreEnvironment' => true, 'noQuerystring' => true)); $loaded[$stylesheetHref] = true; $loaded[$href] = true; try { Q::includeFile($filename); } catch (Exception $e) {} $src = parse_url($href, PHP_URL_PATH); $content = $ob->getClean(); $relativePathPrefix = null; if (!trim($content)) { continue; } $content = Q_Utils::adjustRelativePaths($content, $src, $dest, 'css', $relativePathPrefix); $sheets_for_slots[$stylesheet['slot']][$href] = "\n$content"; $relativePathPrefixes_for_slots[$stylesheet['slot']][$href] = $relativePathPrefix; } } } $parts = array(); foreach ($imported_for_slots as $slot => $imported) { foreach ($imported as $href => $content) { $parts[] = Q_Html::tag( 'style', array('data-slot' => $slot, 'data-href' => $href), $content ); } } foreach ($sheets_for_slots as $slot => $sheets) { foreach ($sheets as $href => $sheet) { $relativePathPrefix = $relativePathPrefixes_for_slots[$slot][$href]; $parts[] = Q_Html::tag( 'style', array( 'data-slot' => $slot, 'data-href' => $href, 'data-path-prefix' => $relativePathPrefix ), $sheet ); } } if ($setLoaded) { self::setScriptData('Q.addStylesheet.loaded', $loaded); } return implode("", $parts); } /** * Returns the HTML markup for referencing all the stylesheets added so far * @method stylesheets * @static. * @param {string} [$slotName=null] If provided, returns only the stylesheets * added while filling this slot. * @param {string} [$between=''] Optional text to insert between the <link> tags * @param {boolean} [$skipPreloads=false] Whether to skip automatically also adding * <link> tags informing the client on what to preload. * @return {string} the HTML markup for referencing all the stylesheets added so far */ static function stylesheets ($slotName = null, $between = "\n", $skipPreloads = false) { if ($slotName === true) { $slotName = null; // for backward compatibility } static $consistent = null; if (!isset($slotName) and $consistent) { return $consistent; } $stylesheets = self::stylesheetsArray($slotName); if (empty($stylesheets)) { return ''; } $preload = Q_Config::get('Q', 'stylesheets', 'preload', null); if ($preload === 'inline' and !Q_Request::isAjax() and !Q_Session::requestedId()) { $result = self::stylesheetsInline($slotName, true); if (!isset($slotName)) { $consistent = $result; } return $consistent; } $baseUrl = Q_Request::baseUrl(); $tags = array(); foreach ($stylesheets as $stylesheet) { $rel = 'stylesheet'; $href = ''; $media = 'screen,print'; $type = 'text/css'; $hash = null; extract($stylesheet, EXTR_IF_EXISTS); $attributes = @compact('rel', 'type', 'href', 'media'); $attributes['data-slot'] = $stylesheet['slot']; $tags[] = Q_Html::tag('link', $attributes, null, @compact('hash')); if (!Q_Request::isAjax() && !Q_Session::requestedId() && $preload === 'header' && Q::ifset(self::$preload, $href, null) && (!Q_Valid::url($href) || Q::startsWith($href, $baseUrl))) { // the command below may fail because response body already started $href_encoded = Q_Utils::urlencodeNonAscii($href); @header("Link: <$href_encoded>; as=style; rel=preload", false); } } $result = implode($between, $tags); if (!isset($slotName)) { $consistent = $result; } return $result; } we also have preload functionality
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)