Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6038)

Unified Diff: chrome/renderer/user_script_slave.cc

Issue 18183: First pass as implementing the greasemonkey API. This patch (Closed)
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | webkit/glue/webframe.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/renderer/user_script_slave.cc
diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc
index 80cc329479ce7c00954351f9661258377cef500b..52d9529768f88d57220af2174a63d0e5ae75b9da 100644
--- a/chrome/renderer/user_script_slave.cc
+++ b/chrome/renderer/user_script_slave.cc
@@ -7,8 +7,14 @@
#include "base/logging.h"
#include "base/pickle.h"
#include "base/shared_memory.h"
+#include "chrome/common/resource_bundle.h"
+#include "chrome/renderer/renderer_resources.h"
#include "googleurl/src/gurl.h"
+// These two strings are injected before and after the Greasemonkey API and
+// user script to wrap it in an anonymous scope.
+static const char kUserScriptHead[] = "(function (unsafeWindow) {";
+static const char kUserScriptTail[] = "\n})(window);";
// UserScript
@@ -110,7 +116,26 @@ std::string UserScript::EscapeGlob(const std::string& input_pattern) {
// UserScriptSlave
-UserScriptSlave::UserScriptSlave() : shared_memory_(NULL) {
+UserScriptSlave::UserScriptSlave()
+ : shared_memory_(NULL),
+ user_script_start_line_(0) {
+ // TODO: Only windows supports resources and only windows supports user
+ // scrips, so only load the Greasemonkey API on windows. Fix this when
+ // better cross platofrm support is available.
+#if defined(OS_WIN)
+ api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
+ IDR_GREASEMONKEY_API_JS);
+#endif
+
+ // Count the number of lines that will be injected before the user script.
+ StringPiece::size_type pos = 0;
+ while ((pos = api_js_.find('\n', pos)) != StringPiece::npos) {
+ user_script_start_line_++;
+ pos++;
+ }
+
+ // Add one more line to account for the function that wraps everything.
+ user_script_start_line_++;
}
bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
@@ -161,8 +186,13 @@ bool UserScriptSlave::InjectScripts(WebFrame* frame) {
for (std::vector<UserScript>::iterator script = scripts_.begin();
script != scripts_.end(); ++script) {
if (script->MatchesUrl(frame->GetURL())) {
- frame->ExecuteJavaScript(script->GetBody().as_string(),
- GURL(script->GetURL().as_string()));
+ std::string inject(kUserScriptHead);
+ inject.append(api_js_.as_string());
+ inject.append(script->GetBody().as_string());
+ inject.append(kUserScriptTail);
+ frame->ExecuteJavaScript(inject,
+ GURL(script->GetURL().as_string()),
+ -user_script_start_line_);
}
}
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | webkit/glue/webframe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698