Index: extensions/renderer/user_script_set.cc |
diff --git a/extensions/renderer/user_script_set.cc b/extensions/renderer/user_script_set.cc |
index 5fd7ab8b28a367829c37239294efccafd442d1e8..cf10081267ea271bb22687c788d792d0f94c1b9b 100644 |
--- a/extensions/renderer/user_script_set.cc |
+++ b/extensions/renderer/user_script_set.cc |
@@ -31,6 +31,11 @@ namespace extensions { |
namespace { |
+// These two strings are injected before and after the Greasemonkey API and |
+// user script to wrap it in an anonymous scope. |
+const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
+const char kUserScriptTail[] = "\n})(window);"; |
+ |
GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { |
GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { |
@@ -113,6 +118,7 @@ bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory, |
CHECK(iter.ReadUInt32(&num_scripts)); |
scripts_.clear(); |
+ script_sources_.clear(); |
scripts_.reserve(num_scripts); |
for (uint32_t i = 0; i < num_scripts; ++i) { |
std::unique_ptr<UserScript> script(new UserScript()); |
@@ -226,4 +232,46 @@ std::unique_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( |
return injection; |
} |
+blink::WebString UserScriptSet::GetJsSource(const UserScript::File& file, |
+ bool emulate_greasemonkey) { |
+ const GURL& url = file.url(); |
+ std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url); |
+ if (iter != script_sources_.end()) |
+ return iter->second; |
+ |
+ base::StringPiece script_content = file.GetContent(); |
+ blink::WebString source; |
+ if (emulate_greasemonkey) { |
+ // We add this dumb function wrapper for user scripts to emulate what |
+ // Greasemonkey does. |script_content| becomes: |
+ // concat(kUserScriptHead, script_content, kUserScriptTail). |
+ std::string content; |
+ content.reserve(strlen(kUserScriptHead) + script_content.length() + |
+ strlen(kUserScriptTail)); |
+ content.append(kUserScriptHead); |
+ script_content.AppendToString(&content); |
+ content.append(kUserScriptTail); |
+ source = blink::WebString::fromUTF8(content); |
+ } else { |
+ source = blink::WebString::fromUTF8(script_content.data(), |
+ script_content.length()); |
+ } |
+ script_sources_[url] = source; |
+ return source; |
+} |
+ |
+blink::WebString UserScriptSet::GetCssSource(const UserScript::File& file) { |
+ const GURL& url = file.url(); |
+ std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url); |
+ if (iter != script_sources_.end()) |
+ return iter->second; |
+ |
+ base::StringPiece script_content = file.GetContent(); |
+ return script_sources_ |
+ .insert(std::make_pair( |
+ url, blink::WebString::fromUTF8(script_content.data(), |
+ script_content.length()))) |
+ .first->second; |
+} |
+ |
} // namespace extensions |