diff --git a/docs/api/session.md b/docs/api/session.md index 199202dd8a96..ad0cb87ac3fc 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -784,6 +784,7 @@ win.webContents.session.setCertificateVerifyProc((request, callback) => { * `webContents` [WebContents](web-contents.md) - WebContents requesting the permission. Please note that if the request comes from a subframe you should use `requestingUrl` to check the request origin. * `permission` string - The type of requested permission. * `clipboard-read` - Request access to read from the clipboard. + * `clipboard-sanitized-write` - Request access to write to the clipboard. * `media` - Request access to media devices such as camera, microphone and speakers. * `display-capture` - Request access to capture the screen. * `mediaKeySystem` - Request access to DRM protected content. diff --git a/spec/chromium-spec.ts b/spec/chromium-spec.ts index 45c28350cdcc..14a9873be0a7 100644 --- a/spec/chromium-spec.ts +++ b/spec/chromium-spec.ts @@ -2547,14 +2547,10 @@ describe('window.getScreenDetails', () => { }); }); -describe('navigator.clipboard', () => { +describe('navigator.clipboard.read', () => { let w: BrowserWindow; before(async () => { - w = new BrowserWindow({ - webPreferences: { - enableBlinkFeatures: 'Serial' - } - }); + w = new BrowserWindow(); await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html')); }); @@ -2599,6 +2595,54 @@ describe('navigator.clipboard', () => { }); }); +describe('navigator.clipboard.write', () => { + let w: BrowserWindow; + before(async () => { + w = new BrowserWindow(); + await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html')); + }); + + const writeClipboard: any = () => { + return w.webContents.executeJavaScript(` + navigator.clipboard.writeText('Hello World!').catch(err => err.message); + `, true); + }; + + after(closeAllWindows); + afterEach(() => { + session.defaultSession.setPermissionRequestHandler(null); + }); + + it('returns clipboard contents when a PermissionRequestHandler is not defined', async () => { + const clipboard = await writeClipboard(); + expect(clipboard).to.not.equal('Write permission denied.'); + }); + + it('returns an error when permission denied', async () => { + session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => { + if (permission === 'clipboard-sanitized-write') { + callback(false); + } else { + callback(true); + } + }); + const clipboard = await writeClipboard(); + expect(clipboard).to.equal('Write permission denied.'); + }); + + it('returns clipboard contents when permission is granted', async () => { + session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => { + if (permission === 'clipboard-sanitized-write') { + callback(true); + } else { + callback(false); + } + }); + const clipboard = await writeClipboard(); + expect(clipboard).to.not.equal('Write permission denied.'); + }); +}); + ifdescribe((process.platform !== 'linux' || app.isUnityRunning()))('navigator.setAppBadge/clearAppBadge', () => { let w: BrowserWindow;