From 5a08522b9812bf9edb468fa71d8d7714f364762b Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Mon, 17 Jun 2019 17:39:36 +0200 Subject: [PATCH] feat: add removeInsertedCSS (#16579) --- atom/renderer/api/atom_api_web_frame.cc | 18 +++++++++++++++--- docs/api/web-contents.md | 23 +++++++++++++++++++++-- docs/api/web-frame.md | 13 ++++++++++++- docs/api/webview-tag.md | 16 ++++++++++++++-- lib/browser/api/web-contents.js | 1 + lib/common/web-view-methods.js | 1 + spec/api-web-contents-spec.js | 10 +++++++++- spec/webview-spec.js | 15 +++++++++++++++ 8 files changed, 88 insertions(+), 9 deletions(-) diff --git a/atom/renderer/api/atom_api_web_frame.cc b/atom/renderer/api/atom_api_web_frame.cc index 483b5bfa3a34..63464d13b337 100644 --- a/atom/renderer/api/atom_api_web_frame.cc +++ b/atom/renderer/api/atom_api_web_frame.cc @@ -325,11 +325,22 @@ void InsertText(v8::Local window, const std::string& text) { } } -void InsertCSS(v8::Local window, const std::string& css) { +base::string16 InsertCSS(v8::Local window, const std::string& css) { blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); if (web_frame->IsWebLocalFrame()) { - web_frame->ToWebLocalFrame()->GetDocument().InsertStyleSheet( - blink::WebString::FromUTF8(css)); + return web_frame->ToWebLocalFrame() + ->GetDocument() + .InsertStyleSheet(blink::WebString::FromUTF8(css)) + .Utf16(); + } + return base::string16(); +} + +void RemoveInsertedCSS(v8::Local window, const base::string16& key) { + blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); + if (web_frame->IsWebLocalFrame()) { + web_frame->ToWebLocalFrame()->GetDocument().RemoveInsertedStyleSheet( + blink::WebString::FromUTF16(key)); } } @@ -546,6 +557,7 @@ void Initialize(v8::Local exports, dict.SetMethod("setSpellCheckProvider", &SetSpellCheckProvider); dict.SetMethod("insertText", &InsertText); dict.SetMethod("insertCSS", &InsertCSS); + dict.SetMethod("removeInsertedCSS", &RemoveInsertedCSS); dict.SetMethod("executeJavaScript", &ExecuteJavaScript); dict.SetMethod("executeJavaScriptInIsolatedWorld", &ExecuteJavaScriptInIsolatedWorld); diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index f104a7d9c28d..d8418a281396 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -987,9 +987,12 @@ Returns `String` - The user agent for this web page. * `css` String -Returns `Promise` +Returns `Promise` - A promise that resolves with a key for the inserted +CSS that can later be used to remove the CSS via +`contents.removeInsertedCSS(key)`. -Injects CSS into the current web page. +Injects CSS into the current web page and returns a unique key for the inserted +stylesheet. ```js contents.on('did-finish-load', function () { @@ -997,6 +1000,22 @@ contents.on('did-finish-load', function () { }) ``` +#### `contents.removeInsertedCSS(key)` + +* `key` String + +Returns `Promise` - Resolves if the removal was successful. + +Removes the inserted CSS from the current web page. The stylesheet is identified +by its key, which is returned from `contents.insertCSS(css)`. + +```js +contents.on('did-finish-load', async function () { + const key = await contents.insertCSS('html, body { background-color: #f00; }') + contents.removeInsertedCSS(key) +}) +``` + #### `contents.executeJavaScript(code[, userGesture])` * `code` String diff --git a/docs/api/web-frame.md b/docs/api/web-frame.md index d0a1609e1256..5354dd0b128a 100644 --- a/docs/api/web-frame.md +++ b/docs/api/web-frame.md @@ -99,7 +99,18 @@ webFrame.setSpellCheckProvider('en-US', { * `css` String - CSS source code. -Inserts `css` as a style sheet in the document. +Returns `String` - A key for the inserted CSS that can later be used to remove +the CSS via `webFrame.removeInsertedCSS(key)`. + +Injects CSS into the current web page and returns a unique key for the inserted +stylesheet. + +### `webFrame.removeInsertedCSS(key)` + +* `key` String + +Removes the inserted CSS from the current web page. The stylesheet is identified +by its key, which is returned from `webFrame.insertCSS(css)`. ### `webFrame.insertText(text)` diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index 04de299ec609..cb2921d38465 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -379,9 +379,21 @@ Returns `String` - The user agent for guest page. * `css` String -Returns `Promise` +Returns `Promise` - A promise that resolves with a key for the inserted +CSS that can later be used to remove the CSS via +`.removeInsertedCSS(key)`. -Injects CSS into the guest page. +Injects CSS into the current web page and returns a unique key for the inserted +stylesheet. + +### `.removeInsertedCSS(key)` + +* `key` String + +Returns `Promise` - Resolves if the removal was successful. + +Removes the inserted CSS from the current web page. The stylesheet is identified +by its key, which is returned from `.insertCSS(css)`. ### `.executeJavaScript(code[, userGesture])` diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index e2abc59696a4..09213075ecd3 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -174,6 +174,7 @@ WebContents.prototype._sendToFrameInternal = function (frameId, channel, ...args const webFrameMethods = [ 'insertCSS', 'insertText', + 'removeInsertedCSS', 'setLayoutZoomLevelLimits', 'setVisualZoomLevelLimits' ] diff --git a/lib/common/web-view-methods.js b/lib/common/web-view-methods.js index 50e68f4e8a9b..9b345195fb32 100644 --- a/lib/common/web-view-methods.js +++ b/lib/common/web-view-methods.js @@ -58,6 +58,7 @@ exports.asyncMethods = new Set([ 'executeJavaScript', 'insertCSS', 'insertText', + 'removeInsertedCSS', 'send', 'sendInputEvent', 'setLayoutZoomLevelLimits', diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 726deadd71f4..203704e0e541 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -515,11 +515,19 @@ describe('webContents module', () => { it('supports inserting CSS', async () => { w.loadURL('about:blank') - w.webContents.insertCSS('body { background-repeat: round; }') + await w.webContents.insertCSS('body { background-repeat: round; }') const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")') expect(result).to.equal('round') }) + it('supports removing inserted CSS', async () => { + w.loadURL('about:blank') + const key = await w.webContents.insertCSS('body { background-repeat: round; }') + await w.webContents.removeInsertedCSS(key) + const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")') + expect(result).to.equal('repeat') + }) + it('supports inspecting an element in the devtools', (done) => { w.loadURL('about:blank') w.webContents.once('devtools-opened', () => { done() }) diff --git a/spec/webview-spec.js b/spec/webview-spec.js index 6c4e077a5f81..703bb26d0551 100644 --- a/spec/webview-spec.js +++ b/spec/webview-spec.js @@ -1010,6 +1010,21 @@ describe(' tag', function () { }) }) + it('supports inserting CSS', async () => { + await loadWebView(webview, { src: `file://${fixtures}/pages/base-page.html` }) + await webview.insertCSS('body { background-repeat: round; }') + const result = await webview.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")') + expect(result).to.equal('round') + }) + + it('supports removing inserted CSS', async () => { + await loadWebView(webview, { src: `file://${fixtures}/pages/base-page.html` }) + const key = await webview.insertCSS('body { background-repeat: round; }') + await webview.removeInsertedCSS(key) + const result = await webview.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")') + expect(result).to.equal('repeat') + }) + describe('sendInputEvent', () => { it('can send keyboard event', async () => { loadWebView(webview, {