diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-17 00:06:27 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-17 00:06:27 +0000 |
commit | 49fed325fbf10a6c612b82360b941d500d8a2fa8 (patch) | |
tree | d4605501b1940f2df25948714a4540a7c8b33b7c /chrome/renderer/user_script_slave.cc | |
parent | 5df5165d0e0d7e6493dbca6d4c98a173a616467d (diff) | |
download | chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.zip chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.tar.gz chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.tar.bz2 |
First pass as implementing the greasemonkey API. This patch
includes a javascript file with stubbed versions of all the
greasemoney API methods as well as changes to the Greasemonkey
slave to inject this javascript file along with other user
scripts into the page.I also have a userscript I'm using for
the unit testing of the API methods. I don't think there is
a good place for it in the chrome tree (if there is I'd like
to know), but you can view it here:
http://skrul.com/greasemonkey/test.user.js
Right now all the tests fail except for testLog and
testUnsafeWindow.
Review URL: http://codereview.chromium.org/18183
Patch from Steve Krulewitz <skrulx@gmail.com>.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8247 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/user_script_slave.cc')
-rw-r--r-- | chrome/renderer/user_script_slave.cc | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc index 80cc329..52d9529 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_); } } |