Skip to content

Commit 29f6509

Browse files
committed
Stamp CI-built obs.min.js with current version
A nice touch for debugging in the wild: add the version number to the minified Obs.js file.
1 parent 94e2f3d commit 29f6509

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Copy/paste the following as close to the top of your `<head>` as possible:
114114

115115
```html
116116
<script>
117-
/*! Obs.js | (c) Harry Roberts, csswizardry.com | MIT */
117+
/*! Obs.js 0.1.2 | (c) Harry Roberts, csswizardry.com | MIT */
118118
;(()=>{const e=document.currentScript;if((!e||e.src||e.type&&"module"===e.type.toLowerCase())&&!1===/^(localhost|127\.0\.0\.1|::1)$/.test(location.hostname))return void console.warn("[Obs.js] Skipping: must be an inline, classic <script> in <head>.",e?e.src?"src="+e.src:"type="+e.type:"type=module");const t=document.documentElement,{connection:n}=navigator;window.obs=window.obs||{};const i=!0===(window.obs&&window.obs.config||{}).observeChanges,o=()=>{const e=window.obs||{},n="number"==typeof e.downlinkBucket?e.downlinkBucket:null;e.connectionCapability="low"===e.rttCategory&&null!=n&&n>=8?"strong":"high"===e.rttCategory||null!=n&&n<=5?"weak":"moderate";const i=!0===e.dataSaver||!0===e.batteryLow;e.conservationPreference=i?"conserve":"neutral",e.deliveryMode=i||"strong"!==e.connectionCapability?i||"weak"===e.connectionCapability?"lite":"cautious":"rich",e.canShowRichMedia="rich"===e.deliveryMode,e.shouldAvoidRichMedia="lite"===e.deliveryMode,["strong","moderate","weak"].forEach(e=>{t.classList.remove(`has-connection-capability-${e}`)}),t.classList.add(`has-connection-capability-${e.connectionCapability}`),["conserve","neutral"].forEach(e=>{t.classList.remove(`has-conservation-preference-${e}`)}),t.classList.add(`has-conservation-preference-${e.conservationPreference}`),["rich","cautious","lite"].forEach(e=>{t.classList.remove(`has-delivery-mode-${e}`)}),t.classList.add(`has-delivery-mode-${e.deliveryMode}`)},a=()=>{if(!n)return;const{saveData:e,rtt:i,downlink:a}=n;window.obs.dataSaver=!!e,t.classList.toggle("has-data-saver",!!e);const s=(e=>Number.isFinite(e)?25*Math.ceil(e/25):null)(i);null!=s&&(window.obs.rttBucket=s);const c=(e=>Number.isFinite(e)?e<75?"low":e<=275?"medium":"high":null)(i);c&&(window.obs.rttCategory=c,["low","medium","high"].forEach(e=>t.classList.remove(`has-latency-${e}`)),t.classList.add(`has-latency-${c}`));const r=(l=a,Number.isFinite(l)?Math.ceil(l):null);var l;if(null!=r){window.obs.downlinkBucket=r;const e=r<=5?"low":r>=8?"high":"medium";window.obs.downlinkCategory=e,["low","medium","high"].forEach(e=>t.classList.remove(`has-bandwidth-${e}`)),t.classList.add(`has-bandwidth-${e}`)}"downlinkMax"in n&&(window.obs.downlinkMax=n.downlinkMax),o()};a(),i&&n&&"function"==typeof n.addEventListener&&n.addEventListener("change",a);const s=e=>{if(!e)return;const{level:n,charging:i}=e,a=Number.isFinite(n)?n<=.05:null;window.obs.batteryCritical=a;const s=Number.isFinite(n)?n<=.2:null;window.obs.batteryLow=s,["critical","low"].forEach(e=>t.classList.remove(`has-battery-${e}`)),s&&t.classList.add("has-battery-low"),a&&t.classList.add("has-battery-critical");const c=!!i;window.obs.batteryCharging=c,t.classList.toggle("has-battery-charging",c),o()};"getBattery"in navigator&&navigator.getBattery().then(e=>{s(e),i&&"function"==typeof e.addEventListener&&(e.addEventListener("levelchange",()=>s(e)),e.addEventListener("chargingchange",()=>s(e)))}).catch(()=>{})})();
119119
//# sourceURL=obs.inline.js
120120
</script>

scripts/build.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { minify } from 'terser';
33

4+
// In CI (i.e. tag builds), GitHub sets this to e.g. ‘0.1.1’
5+
const CI_VERSION = process.env.GITHUB_REF_NAME || '';
46

57
// Minimal build: read obs.js → write obs.min.js (root)
68
const INPUT = 'obs.js';
@@ -20,9 +22,8 @@ const result = await minify(src, {
2022
});
2123
if (!result.code) throw new Error('Minify failed for obs.js');
2224

23-
24-
// Leading ';' to guard against ASI, tiny banner for provenance
25-
const header = '/*! Obs.js | (c) Harry Roberts, csswizardry.com | MIT */\n;';
25+
// Stamp current tag in CI, omit locally; leading ';' to guard against ASI.
26+
const header = `/*! Obs.js${CI_VERSION && ' ' + CI_VERSION} | (c) Harry Roberts, csswizardry.com | MIT */\n;`;
2627
const footer = '\n//# sourceURL=obs.inline.js';
2728
await writeFile(OUTPUT, header + result.code + footer, 'utf8');
2829
console.log(`[obs] Wrote ${OUTPUT}`);

0 commit comments

Comments
 (0)