diff options
author | hanxi <hanxi@chromium.org> | 2015-05-06 11:58:29 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-06 18:59:02 +0000 |
commit | 55723f49915d2992d850c124f90c1f9084ab1a6a (patch) | |
tree | 8731f7e3fdc0ff7d5291de8fc44ca3232ac92e5c /extensions | |
parent | 7573e42dc9fcb20f31f980d5f1fa8125719ce0f0 (diff) | |
download | chromium_src-55723f49915d2992d850c124f90c1f9084ab1a6a.zip chromium_src-55723f49915d2992d850c124f90c1f9084ab1a6a.tar.gz chromium_src-55723f49915d2992d850c124f90c1f9084ab1a6a.tar.bz2 |
Make "code" property be a part of the "css" and "js" properties of webview.addContentScripts API.
With this patch, users can specify code or files of JavaScript or CSS to inject.
Now the API looks like:
<webview>.addContentScripts([
{
name: 'myRule',
matches: ['http://www.foo.com/*'],
css: {
files: ['mystyles.css']
},
js: {
code: 'document.body.style.backgroundColor = \'red\';'
}
}]);
BUG=461052
Review URL: https://codereview.chromium.org/1128213002
Cr-Commit-Position: refs/heads/master@{#328571}
Diffstat (limited to 'extensions')
3 files changed, 63 insertions, 50 deletions
diff --git a/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc b/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc index 0627e78..56eb6d6 100644 --- a/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc +++ b/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc @@ -25,6 +25,7 @@ using content::WebContents; using extensions::ExtensionResource; using extensions::core_api::web_view_internal::ContentScriptDetails; +using extensions::core_api::web_view_internal::InjectionItems; using extensions::core_api::web_view_internal::SetPermission::Params; using extensions::core_api::extension_types::InjectDetails; using extensions::UserScript; @@ -79,6 +80,35 @@ HostID GenerateHostIDFromEmbedder(const extensions::Extension* extension, return HostID(); } +// Creates content script files when parsing InjectionItems of "js" or "css" +// proterties, and stores them in the |result|. +void AddScriptFiles(const GURL& owner_base_url, + const extensions::Extension* extension, + const InjectionItems& items, + UserScript::FileList* result) { + // files: + if (items.files) { + for (const std::string& relative : *items.files) { + GURL url = owner_base_url.Resolve(relative); + if (extension) { + ExtensionResource resource = extension->GetResource(relative); + result->push_back(UserScript::File(resource.extension_root(), + resource.relative_path(), url)); + } else { + result->push_back(extensions::UserScript::File(base::FilePath(), + base::FilePath(), url)); + } + } + } + // code: + if (items.code) { + extensions::UserScript::File file((base::FilePath()), (base::FilePath()), + GURL()); + file.set_content(*items.code); + result->push_back(file); + } +} + // Parses the values stored in ContentScriptDetails, and constructs a // UserScript. bool ParseContentScript(const ContentScriptDetails& script_value, @@ -146,55 +176,29 @@ bool ParseContentScript(const ContentScriptDetails& script_value, // css: if (script_value.css) { - for (const std::string& relative : *(script_value.css.get())) { - GURL url = owner_base_url.Resolve(relative); - if (extension) { - ExtensionResource resource = extension->GetResource(relative); - script->css_scripts().push_back(UserScript::File( - resource.extension_root(), resource.relative_path(), url)); - } else { - script->css_scripts().push_back(extensions::UserScript::File( - base::FilePath(), base::FilePath(), url)); - } - } + AddScriptFiles(owner_base_url, extension, *script_value.css, + &script->css_scripts()); } // js: if (script_value.js) { - for (const std::string& relative : *(script_value.js.get())) { - GURL url = owner_base_url.Resolve(relative); - if (extension) { - ExtensionResource resource = extension->GetResource(relative); - script->js_scripts().push_back(UserScript::File( - resource.extension_root(), resource.relative_path(), url)); - } else { - script->js_scripts().push_back(extensions::UserScript::File( - base::FilePath(), base::FilePath(), url)); - } - } - } - - // code: - if (script_value.code) { - extensions::UserScript::File file((base::FilePath()), (base::FilePath()), - GURL()); - file.set_content(*(script_value.code.get())); - script->js_scripts().push_back(file); + AddScriptFiles(owner_base_url, extension, *script_value.js, + &script->js_scripts()); } // all_frames: if (script_value.all_frames) - script->set_match_all_frames(*(script_value.all_frames)); + script->set_match_all_frames(*script_value.all_frames); // include_globs: if (script_value.include_globs) { - for (const std::string& glob : *(script_value.include_globs.get())) + for (const std::string& glob : *script_value.include_globs) script->add_glob(glob); } // exclude_globs: if (script_value.exclude_globs) { - for (const std::string& glob : *(script_value.exclude_globs.get())) + for (const std::string& glob : *script_value.exclude_globs) script->add_exclude_glob(glob); } diff --git a/extensions/common/api/web_view_internal.json b/extensions/common/api/web_view_internal.json index 044bbe2..e2471ac 100644 --- a/extensions/common/api/web_view_internal.json +++ b/extensions/common/api/web_view_internal.json @@ -95,6 +95,24 @@ "enum": ["allow", "deny", "default"] }, { + "id": "InjectionItems", + "type": "object", + "description": "The type of injection item: code or a set of files.", + "properties": { + "code": { + "type": "string", + "optional": true, + "description": "JavaScript code or CSS to be injected into matching pages." + }, + "files": { + "type": "array", + "items": { "type": "string"}, + "optional": true, + "description": "The list of JavaScript or CSS files to be injected into matching pages. These are injected in the order they appear in this array." + } + } + }, + { "id": "ContentScriptDetails", "type": "object", "description": "Details of the content script to inject.", @@ -120,21 +138,14 @@ "description": "Whether to insert the content script on about:blank and about:srcdoc. Content scripts will only be injected on pages when their inherit URL is matched by one of the declared patterns in the matches field. The inherit URL is the URL of the document that created the frame or window. Content scripts cannot be inserted in sandboxed frames." }, "css": { - "type": "array", - "items": { "type": "string"}, + "$ref": "InjectionItems", "optional": true, - "description": "The list of CSS files to be injected into matching pages. These are injected in the order they appear in this array, before any DOM is constructed or displayed for the page." + "description": "The CSS code or a list of CSS files to be injected into matching pages. These are injected in the order they appear, before any DOM is constructed or displayed for the page." }, "js": { - "type": "array", - "items": { "type": "string"}, - "optional": true, - "description": "The list of JavaScript files to be injected into matching pages. These are injected in the order they appear in this array." - }, - "code": { - "type": "string", + "$ref": "InjectionItems", "optional": true, - "description": "JavaScript code to be injected into matching pages." + "description": "The JavaScript code or a list of JavaScript files to be injected into matching pages. These are injected in the order they appear." }, "run_at": { "$ref": "extensionTypes.RunAt", diff --git a/extensions/renderer/resources/guest_view/web_view/web_view_api_methods.js b/extensions/renderer/resources/guest_view/web_view/web_view_api_methods.js index 835eb27..e939e5d 100644 --- a/extensions/renderer/resources/guest_view/web_view/web_view_api_methods.js +++ b/extensions/renderer/resources/guest_view/web_view/web_view_api_methods.js @@ -95,9 +95,8 @@ var WEB_VIEW_API_METHODS = [ // ----------------------------------------------------------------------------- // Custom API method implementations. -WebViewImpl.prototype.addContentScripts = function() { - var args = $Array.concat([this.viewInstanceId], $Array.slice(arguments)); - return $Function.apply(WebViewInternal.addContentScripts, null, args); +WebViewImpl.prototype.addContentScripts = function(rules) { + return WebViewInternal.addContentScripts(this.viewInstanceId, rules); }; WebViewImpl.prototype.back = function(callback) { @@ -159,9 +158,8 @@ WebViewImpl.prototype.print = function() { return this.executeScript({code: 'window.print();'}); }; -WebViewImpl.prototype.removeContentScripts = function(var_args) { - var args = $Array.concat([this.viewInstanceId], $Array.slice(arguments)); - return $Function.apply(WebViewInternal.removeContentScripts, null, args); +WebViewImpl.prototype.removeContentScripts = function(names) { + return WebViewInternal.removeContentScripts(this.viewInstanceId, names); }; WebViewImpl.prototype.setUserAgentOverride = function(userAgentOverride) { |