diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index d5ecf0aa1d96..64f3e2f5c6bc 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -987,13 +987,13 @@ Returns `String` - The user agent for this web page. **[Deprecated](modernization/property-updates.md)** -#### `contents.insertCSS(css)` +#### `contents.insertCSS(css[, options])` * `css` String +* `options` Object (optional) + * `cssOrigin` String (optional) - Can be either 'user' or 'author'; Specifying 'user' enables you to prevent websites from overriding the CSS you insert. Default is 'author'. -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)`. +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 and returns a unique key for the inserted stylesheet. diff --git a/shell/renderer/api/atom_api_web_frame.cc b/shell/renderer/api/atom_api_web_frame.cc index 13829d332ee0..ac6ca330e15a 100644 --- a/shell/renderer/api/atom_api_web_frame.cc +++ b/shell/renderer/api/atom_api_web_frame.cc @@ -61,6 +61,25 @@ struct Converter { } }; +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + blink::WebDocument::CSSOrigin* out) { + std::string css_origin; + if (!ConvertFromV8(isolate, val, &css_origin)) + return false; + if (css_origin == "user") { + *out = blink::WebDocument::CSSOrigin::kUserOrigin; + } else if (css_origin == "author") { + *out = blink::WebDocument::CSSOrigin::kAuthorOrigin; + } else { + return false; + } + return true; + } +}; + } // namespace mate namespace electron { @@ -325,12 +344,21 @@ void InsertText(v8::Local window, const std::string& text) { } } -base::string16 InsertCSS(v8::Local window, const std::string& css) { +base::string16 InsertCSS(v8::Local window, + const std::string& css, + mate::Arguments* args) { + blink::WebDocument::CSSOrigin css_origin = + blink::WebDocument::CSSOrigin::kAuthorOrigin; + + mate::Dictionary options; + if (args->GetNext(&options)) + options.Get("cssOrigin", &css_origin); + blink::WebFrame* web_frame = GetRenderFrame(window)->GetWebFrame(); if (web_frame->IsWebLocalFrame()) { return web_frame->ToWebLocalFrame() ->GetDocument() - .InsertStyleSheet(blink::WebString::FromUTF8(css)) + .InsertStyleSheet(blink::WebString::FromUTF8(css), nullptr, css_origin) .Utf16(); } return base::string16();