8000 feat: add removeInsertedCSS by brenca · Pull Request #16579 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add removeInsertedCSS #16579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions atom/renderer/api/atom_api_web_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,22 @@ void InsertText(v8::Local<v8::Value> window, const std::string& text) {
}
}

void InsertCSS(v8::Local<v8::Value> window, const std::string& css) {
base::string16 InsertCSS(v8::Local<v8::Value> 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<v8::Value> 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));
}
}

Expand Down Expand Up @@ -546,6 +557,7 @@ void Initialize(v8::Local<v8::Object> 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);
Expand Down
23 changes: 21 additions & 2 deletions docs/api/web-contents.md
-->
Original file line number Diff line number Diff line change
Expand Up @@ -987,16 +987,35 @@ Returns `String` - The user agent for this web page.

* `css` String

Returns `Promise<void>`
Returns `Promise<String>` - 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 () {
contents.insertCSS('html, body { background-color: #f00; }')
})
```

#### `contents.removeInsertedCSS(key)`

* `key` String

Returns `Promise<void>` - 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
Expand Down
13 changes: 12 additions & 1 deletion docs/api/web-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down
16 changes: 14 additions & 2 deletions docs/api/webview-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,21 @@ Returns `String` - The user agent for guest page.

* `css` String

Returns `Promise<void>`
Returns `Promise<String>` - A promise that resolves with a key for the inserted
CSS that can later be used to remove the CSS via
`<webview>.removeInsertedCSS(key)`.

Injects CSS into the current web page and returns a unique key for the inserted
stylesheet.

### `<webview>.removeInsertedCSS(key)`

* `key` String

Returns `Promise<void>` - Resolves if the removal was successful.

Injects CSS into the guest page.
Removes the inserted CSS from the current web page. The stylesheet is identified
by its key, which is returned from `<webview>.insertCSS(css)`.

### `<webview>.executeJavaScript(code[, userGesture])`

Expand Down
1 change: 1 addition & 0 deletions lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ WebContents.prototype._sendToFrameInternal = function (frameId, channel, ...args
const webFrameMethods = [
'insertCSS',
'insertText',
'removeInsertedCSS',
'setLayoutZoomLevelLimits',
'setVisualZoomLevelLimits'
]
Expand Down
1 change: 1 addition & 0 deletions lib/common/web-view-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ exports.asyncMethods = new Set([
'executeJavaScript',
'insertCSS',
'insertText',
'removeInsertedCSS',
'send',
'sendInputEvent',
'setLayoutZoomLevelLimits',
Expand Down
10 changes: 9 additions & 1 deletion spec/api-web-contents-spec.js
Original file line number Diff line numbe AE96 r Diff line change
Expand Up @@ -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() })
Expand Down
15 changes: 15 additions & 0 deletions spec/webview-spec.js
400E
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,21 @@ describe('<webview> 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, {
Expand Down
0