From d84a9b95fb55a1876eeea508f649b053799c1425 Mon Sep 17 00:00:00 2001 From: Nikolai Ommundsen Date: Fri, 24 Aug 2018 11:11:56 +0200 Subject: [PATCH] Transfered Web APIs to another file --- types/chrome-apps/appview.d.ts | 2 +- types/chrome-apps/index.d.ts | 46 ++++++++--- types/chrome-apps/test/index.ts | 82 ++++++++++++++++++- types/chrome-apps/tsconfig.json | 8 +- types/chrome-apps/web-apis.d.ts | 140 ++++++++++++++++++++++++++++++++ 5 files changed, 258 insertions(+), 20 deletions(-) create mode 100644 types/chrome-apps/web-apis.d.ts diff --git a/types/chrome-apps/appview.d.ts b/types/chrome-apps/appview.d.ts index 76fe7ccf61..9e88d67277 100644 --- a/types/chrome-apps/appview.d.ts +++ b/types/chrome-apps/appview.d.ts @@ -1,6 +1,6 @@ // Type definitions for Chrome packaged application development // Project: http://developer.chrome.com/apps/ -// Definitions by: Nikolai Ommundsen , Adam Lay , MIZUNE Pine , MIZUSHIMA Junki , Ingconst Stepanyan , Adam Pyle , Matthew Kimber , otiai10 , couven92 , RReverser , sreimer15 +// Definitions by: Nikolai Ommundsen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 diff --git a/types/chrome-apps/index.d.ts b/types/chrome-apps/index.d.ts index 7ce488deeb..70ed090066 100644 --- a/types/chrome-apps/index.d.ts +++ b/types/chrome-apps/index.d.ts @@ -7,6 +7,7 @@ /// /// /// +/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // WebView ref // @@ -9943,33 +9944,53 @@ declare namespace chrome { * The chrome.types API contains type declarations for Chrome. */ namespace types { + /** + * The scope of the ChromeSetting. One of + * • regular: setting for the regular profile (which is inherited by the incognito profile if not overridden elsewhere), + * • regular_only: setting for the regular profile only (not inherited by the incognito profile), + * • incognito_persistent: setting for the incognito profile that survives browser restarts (overrides regular preferences), + * • incognito_session_only: setting for the incognito profile that can only be set during an incognito session and is deleted + * when the incognito session ends (overrides regular and incognito_persistent preferences). + */ + type ChromeSettingScope = "regular" | "regular_only" | "incognito_persistent" | "incognito_session_only"; + /** + * One of + * • not_controllable: cannot be controlled by any extension + * • controlled_by_other_extensions: controlled by extensions with higher precedence + * • controllable_by_this_extension: can be controlled by this app + * • controlled_by_this_extension: controlled by this app + */ + type LevelOfControl = "not_controllable" | "controlled_by_other_extensions" | "controllable_by_this_extension" | "controlled_by_this_extension"; + interface ChromeSettingClearDetails { /** - * Optional. * The scope of the ChromeSetting. One of * • regular: setting for the regular profile (which is inherited by the incognito profile if not overridden elsewhere), * • regular_only: setting for the regular profile only (not inherited by the incognito profile), * • incognito_persistent: setting for the incognito profile that survives browser restarts (overrides regular preferences), - * • incognito_session_only: setting for the incognito profile that can only be set during an incognito session and is deleted when the incognito session ends (overrides regular and incognito_persistent preferences). + * • incognito_session_only: setting for the incognito profile that can only be set during an incognito session and is deleted + * when the incognito session ends (overrides regular and incognito_persistent preferences). */ - scope?: string; + scope?: ChromeSettingScope; } interface ChromeSettingSetDetails extends ChromeSettingClearDetails { /** * The value of the setting. - * Note that every setting has a specific value type, which is described together with the setting. An extension should not set a value of a different type. + * Note that every setting has a specific value type, + * which is described together with the setting. + * An app should not set a value of a different type. */ value: any; /** - * Optional. * The scope of the ChromeSetting. One of * • regular: setting for the regular profile (which is inherited by the incognito profile if not overridden elsewhere), * • regular_only: setting for the regular profile only (not inherited by the incognito profile), * • incognito_persistent: setting for the incognito profile that survives browser restarts (overrides regular preferences), - * • incognito_session_only: setting for the incognito profile that can only be set during an incognito session and is deleted when the incognito session ends (overrides regular and incognito_persistent preferences). + * • incognito_session_only: setting for the incognito profile that can only be set during an incognito session and is deleted + * when the incognito session ends (overrides regular and incognito_persistent preferences). */ - scope?: string; + scope?: ChromeSettingScope; } interface ChromeSettingGetDetails { @@ -9983,6 +10004,8 @@ declare namespace chrome { type DetailsCallback = (details: ChromeSettingGetResultDetails) => void; interface ChromeSettingGetResultDetails { + /** The value of the setting. */ + value: any; /** * One of * • not_controllable: cannot be controlled by any extension @@ -9991,10 +10014,7 @@ declare namespace chrome { * • controlled_by_this_extension: controlled by this app */ levelOfControl: string; - /** The value of the setting. */ - value: any; /** - * Optional. * Whether the effective value is specific to the incognito session. * This property will only be present if the incognito property in the details parameter of get() was true. */ @@ -10010,7 +10030,7 @@ declare namespace chrome { * @param details Which setting to change. * @param callback Called at the completion of the set operation. */ - set(details: ChromeSettingSetDetails, callback?: Function): void; + set(details: ChromeSettingSetDetails, callback?: () => void): void; /** * Gets the value of a setting. * @param details Which setting to consider. @@ -10021,7 +10041,7 @@ declare namespace chrome { * @param details Which setting to clear. * @param callback Called at the completion of the clear operation. */ - clear(details: ChromeSettingClearDetails, callback?: Function): void; + clear(details: ChromeSettingClearDetails, callback?: () => void): void; /** Fired after the setting changes. */ onChange: ChromeSettingChangedEvent; } @@ -11421,7 +11441,7 @@ declare namespace chrome { //////////// // EXPORT // //////////// -interface Window { +declare interface Window extends ChromeWindow { chrome: typeof chrome; WebView: typeof HTMLWebViewElement; AppView: typeof HTMLAppViewElement; diff --git a/types/chrome-apps/test/index.ts b/types/chrome-apps/test/index.ts index 30d20c408d..ff5473f218 100644 --- a/types/chrome-apps/test/index.ts +++ b/types/chrome-apps/test/index.ts @@ -8,9 +8,87 @@ import runtime = chrome.app.runtime; const cwindow = chrome.app.window; // #region FORBIDDEN APIs +// Will give warnings via IntelliSense. + +localStorage.getItem('hei'); +document.open(); +document.close(); +document.write(); document.write('forbidden'); -Document.prototype.write.call(document, 'Hello, world'); -window.addEventListener('beforeunload', () => { }); + +// #endregion + +// #region Web APIs + +/// +/// HTML5 Audio +/// + +var audioCtx = new window.AudioContext(); // define audio context +// Webkit/blink browsers need prefix, Safari won't work without window. + +var drawVisual; // requestAnimationFrame + +var analyser = audioCtx.createAnalyser(); +var distortion = audioCtx.createWaveShaper(); +var gainNode = audioCtx.createGain(); +var biquadFilter = audioCtx.createBiquadFilter(); + +navigator.getUserMedia({ + audio: true +}, + (stream) => { + const source = audioCtx.createMediaStreamSource(stream); + source.connect(analyser); + analyser.connect(distortion); + distortion.connect(biquadFilter); + biquadFilter.connect(gainNode); + gainNode.connect(audioCtx.destination); // connecting the different audio graph nodes together + }, + (error) => { + console.error(error); + } +); + + +/// +/// HTML5 Canvas +/// + +const canvas = document.createElement('canvas'); +const ctx = canvas.getContext('2d'); +if (ctx) { + ctx.fillStyle = 'green'; + ctx.fillRect(10, 10, 100, 100); +} + +/// +/// Fullscreen API +/// + +const elem = document.createElement('video'); +if (elem.requestFullscreen) { + elem.requestFullscreen(); +} +document.webkitCancelFullScreen() + +/// +/// Geolocation API +/// + +window.onload = () => { + navigator.geolocation.getCurrentPosition((position) => { + console.log('lat', position.coords.latitude, 'lon', position.coords.longitude); + }); +}; + +/// +/// WebKit APIs +/// + +const mediaStream = new window.webkitMediaStream(); +const rtcPeerConnection = new window.webkitRTCPeerConnection(); + // #endregion // #region Manifest diff --git a/types/chrome-apps/tsconfig.json b/types/chrome-apps/tsconfig.json index 50a50c3193..6e00d1a959 100644 --- a/types/chrome-apps/tsconfig.json +++ b/types/chrome-apps/tsconfig.json @@ -1,10 +1,6 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es2017", - "dom" - ], "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, @@ -13,6 +9,10 @@ "typeRoots": [ "../" ], + "lib": [ + "es2017", + "dom" + ], "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true diff --git a/types/chrome-apps/web-apis.d.ts b/types/chrome-apps/web-apis.d.ts new file mode 100644 index 0000000000..6b2c06cf96 --- /dev/null +++ b/types/chrome-apps/web-apis.d.ts @@ -0,0 +1,140 @@ +// Type definitions for Chrome packaged application development +// Project: http://developer.chrome.com/apps/ +// Definitions by: Nikolai Ommundsen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +///////////////////////// +// WEB APIs & Warnings // +///////////////////////// +/** + * Only warnings since: + * 1. It's not possible as of now to override or remove something from *Window* or *Document*. + * 2. It may also cause other unforseen consequences if it's removed (and if it was possible). + * @see[Disallowed] + */ +/** + * Chrome app - Web APIs + * @see[Docs]{@link https://developer.chrome.com/apps/api_other} + */ +interface ChromeWindow { + /// + /// Disabled Web Features + /// https://developer.chrome.com/apps/app_deprecated + /// + + /** + * ❗ alert is not available in packaged apps. ❗ + * Work-around: Use a custom lightbox / popup. + */ + alert(message?: any): void; + /** + * ❗ confirm is not available in packaged apps. ❗ + * Work-around: Use a custom lightbox / popup. + */ + confirm(message?: string): boolean; + + /** + * ❗ window.location is not available in packaged apps. ❗ + * Links open up with the system web browser. + */ + location: Location; + /** + * ❗ window.history is not available in packaged apps. ❗ + * Links open up with the system web browser. + */ + readonly history: History; + + /// + /// Other APIs + /// + AudioContext: typeof AudioContext; + ////////////////////////// + // WebKit prefixed APIs // + ////////////////////////// + webkitMediaStream: typeof MediaStream; + webkitRTCPeerConnection: typeof RTCPeerConnection; +} + +interface Document { + /** + * ❗ document.cookie is not available in packaged apps. ❗ + * Packaged app pages are not rendered on the server, so there is no need to use these. + */ + cookie: string; + /** + * ❗ document.close is not available in packaged apps. ❗ + */ + close(): void; + /** + * ❗ document.open is not available in packaged apps. ❗ + */ + open(url?: string, name?: string, features?: string, replace?: boolean): Document; + /** + * ❗ document.write is not available in packaged apps. ❗ + */ + write(...content: string[]): void; +} + +/** + * ❗ iframes are not available in packaged apps, use webviews instead. ❗ + */ +interface HTMLIFrameElement { } + +/** + * ❗ Modal dialogs are not available in packaged apps, use lightbox/popup instead. ❗ + */ +interface HTMLDialogElement { } + +interface HTMLElement { + /** + * @requires Permissions: 'app.window.fullscreen', 'app.window.fullscreen.overrideEsc' + * @description + * In Chrome Apps, fullscreen is entered without prompting the user or providing + * exit instructions. HTML5 fullscreen requires the app.window.fullscreen permission + * in the manifest. In normal webpages, the browser intercepts the ESC key to exit + * pointer lock ensuring a consistent escape method for users. That is also the + * behavior in Chrome Apps unless the app.window.fullscreen.overrideEsc permission + * is used to enable the app to call preventDefault on keydown and keyup events. + * + * Then to exit fullscreen, the document exposes a method for that: + * @example + * document.webkitExitFullscreen(); + */ + webkitRequestFullscreen(): void; +} + +interface HTMLElement { + /** + * ❗ Unprefixed version are not available as of Chrome 68, in Chrome apps ❗ + */ + requestFullscreen(): void; + /** + * ❗ Unprefixed version are not available as of Chrome 68, in Chrome apps ❗ + */ + exitrequestFullscreen(): void; + /** + * @requires Permissions: 'pointerLock' + */ + requestPointerLock(): void; + /** + * @requires Permissions: 'pointerLock' + */ + exitPointerLock(): void; +} + +interface Navigator { + /** + * If you provide the 'geolocation' in your Chrome app it will allow the app to + * use the proposed HTML5 geolocation API without prompting the user for permission. + * @see Permissions: 'geolocation' + */ + readonly geolocation: Geolocation; +} + + + +/** + * ❗ window.localStorage is not available in packaged apps. Use chrome.storage.local instead. ❗ + */ +declare var localStorage: typeof localStorage;