Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chrome Extension #151

Merged
merged 8 commits into from
Aug 4, 2015

Conversation

sh19910711
Copy link

This pull-request contains Chrome Extension and some preparations for the browser extensions.

1. Use a static method to send XHRs
  • Use a static method to send XHRs (ref: 937d220)
    • Add "REPLConsole.request()"
      • We can use this static method from instance methods and external scripts
      • And we can override this method by reassigning if needed
    • Add "REPLConsole.XMLHttpRequest" for browser extensions
  • Add tests for REPLConsole#commandHandle() (ref: 20ae7e5)
    • This is also testing REPLConsole.request()
2. Other preparations for the browser extensions
  • Add support for CommonJS (ref: c820b09)
    • For the Firefox Extension.
  • Add full-screen mode for browser extensions (ref: 4cf06fb)
    • CSS-based
    • For the browser extensions
3. Add Chrome Extension
  • Add source code of Chrome Extension (ref: 84329e0)
    • Show the console in the DevTools
  • Create a build environment for Chrome Extension (ref: 63279d9)
    • Use Gulp to build the browser extensions

About the extension, we can try the development version by the following commands:

$ git clone https://github.com/rails/web-console.git
$ cd web-console/brx
$ bundle install
$ bundle exec rake ext:chrome:run

Now I'm looking for an edge case for the extension, so welcome any feedback from anyone at any time.

And hello @gsamokovarov, could you review this pull-request, please?

Thank you.

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from 63279d9 to 5366360 Compare July 23, 2015 17:46
@@ -0,0 +1,66 @@
# Tasks to build chrome extension.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we build the extensions with Ruby tooling? E.g. rake.

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch 3 times, most recently from 0a62f07 to f60f126 Compare July 26, 2015 10:01
@sh19910711
Copy link
Author

Updated:

  • Rename directories
    • brx/ => extensions/
    • crx/ => chrome/
  • Add FakeMiddleware class for testing and the browser extensions (ref: af0301b)
  • Add a feature to hide an existing console element on the page when the console is displayed in the panel of Chrome DevTools
    • For that, add REPLConsole#uninstall() (ref: 2f44b6a)

The Rakefile is not finished yet ✋

GIF - Hide existing console

GIF

Thanks.

@gsamokovarov
Copy link
Collaborator

Great! Thanks for the update.

@simkessy
Copy link

This looks really awesome. I'm running rails through Vagrant through, so Chrome is on a localhost and rails is running in a VM. How would I set this up on my current system?

@sh19910711
Copy link
Author

@simkessy Thank you. The extension can detect Web Console by a response header, so I think there is no need to set up if you see the default console. All you have to do is to install the extension (and you might need to do gem "web-console", "~>2.2.0" or something perhaps).

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from f60f126 to 563319e Compare July 27, 2015 13:04
@sh19910711
Copy link
Author

Updated:

  • Use Rake instead of Gulp to build the browser extensions
  • Add rake tasks for the Chrome Extension
    • ext:chrome is to build the extension
    • ext:chrome:run: is to launch a browser with the extension

@gsamokovarov Hello, could you review the rake tasks again? (ref: 563319e)

Thank you!

require "web_console"

module WebConsole
class FakeMiddleware
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this live in lib/web_console/testing/fake_middleware.rb? This is to follow the example of other Rails component grouping their testing utilities in similar module.

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from 563319e to 59fe2f3 Compare July 28, 2015 08:48
@sh19910711
Copy link
Author

Updated:

  • Move erb_helper.rb and fake_middleware.rb into lib/web_console/testing/
  • Add ERBHelper class to pre-compile templates (ref: 59fe2f3)
    • We can build the extension faster!

@gsamokovarov Any other points you have?

Thanks.

"crx": "^3.0.2"
},
"scripts": {
"crx": "npm install && node \"$(npm bin)/gulp\" crx/run"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have gulp anymore, right?

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from 59fe2f3 to ae8bbde Compare July 28, 2015 11:21
@sh19910711
Copy link
Author

Updated again:

  • Rename scripts => script
  • Move FakeMiddleware into the top-level (it means WebConsole::FakeMiddleware => FakeMiddleware)
  • Squash commits of the rake tasks

Thanks again.

require 'web_console/testing/fake_middleware'

# This class is to pre-compile 'templates/*.erb'.
class ERBHelper
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERBPrecompiler

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, please put it inside of the WebConsole::Testing namespace. Its more predictable to have the web_console/testing/erb_helper class/module in this namespace. You can call it with the fully qualified name wherever its referenced now. Same applies to the FakeMiddleware.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will do it :-)

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from ae8bbde to 0f64708 Compare July 29, 2015 04:56
@sh19910711
Copy link
Author

Updated:

  • Do callback(result, response) in the REPLConsole#commandHandle() (ref: 5d66fa3)
  • Fix the new hash syntax in the Rakefile ✂️
  • Use WebConsole::Testing
  • And rename ERBHelper to ERBPrecompiler

Thanks!

@sh19910711
Copy link
Author

Forgot to write, I added WebConsole.gem_root into the lib/web_console.rb to get path from root directory (ref: 0f64708#diff-4ffc3efe29c6bb1b76c331c9cffe6b67R22).

require "action_dispatch"
require "web_console"

module WebConsole::Testing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer it written like:

module WebConsole
  module Testing
    class FakeMiddleware
    end
  end
end

That way, the file that is required first will create the WebConsole::Testing module and the other files will just reopen it. You also get better Module.nesting and can reference constants in WebConsole relatively as well. Same for the precompiler.

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from 0f64708 to 221da56 Compare July 29, 2015 09:56
@sh19910711
Copy link
Author

Updated again.

I'd prefer gem_root to just return a Pathname object. If you need to join on it, you can call #join.

Yeah, that makes sense.

You also get better Module.nesting and can reference constants in WebConsole relatively as well. Same for the precompiler.

Now I get it.

Thanks!


task build: [ extdir, 'lib:templates' ] do
cd rootdir do
cp_r [ 'chrome/html', 'chrome/js', 'chrome/manifest.json', 'img/', 'tmp/lib/' ], dist
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line ends up copying files which should be ignored (e.g., vim swap files). I will rewrite this task with git ls-files or something else.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch 2 times, most recently from f709292 to 49acec6 Compare July 31, 2015 05:30
@sh19910711
Copy link
Author

Updated (diff for the Rakefile: 221da56...2d350dc):

Thanks!

@sh19910711
Copy link
Author

Don't require "web-console" on fake_middleware.rb

Oh, this also causes NoMethodError for the WebConsole.gem_root. I will fix it.

@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from 49acec6 to f79075b Compare July 31, 2015 08:23
@sh19910711
Copy link
Author

Updated again:

  • Move WebConsole.gem_root to WebConsole::Testing::Helper.gem_root (diff is here: 33e6a59)
    • It means to limit the access to the method for testing and similar things

And thanks again.

cp_r [ 'img/', 'tmp/lib/' ], dist
`cd chrome && git ls-files`.split("\n").each do |src|
dest = dist.join(src)
mkdir_p dest.dirname unless Dir.exists?(dest.dirname)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mkdir_p will check if the dir exist and won't create it at all, so you can save yourself the condition.

For the development:

```
$ git clone https://github.com/rails/web-console.git
$ cd web-console
$ bundle install
$ bundle exec rake ext:chrome:run
=> Chrome will be launched with the extension.
```

And provide some rake tasks for the extension:

- "ext:chrome" is to build the extension
- "ext:chrome:run" is to launch a browser with the extension
- "ext:chrome:zip" is to generate .zip
- "ext:chrome:crx" is to generate .crx
@sh19910711 sh19910711 force-pushed the patch/0017/chrome-extension branch from f79075b to 7098a7e Compare July 31, 2015 09:15
@sh19910711
Copy link
Author

Updated again*2 (diff: f79075b...ece32ec):

  • Remove the unless condition
  • Fix indent of private methods of the FakeMiddleware class (>>)
  • Remove attr_readers of the FakeMiddleware class

@gsamokovarov
Copy link
Collaborator

@sh19910711 Do you have anything to add here? There are areas which we can improve, but I think it's a decent start.

@sh19910711
Copy link
Author

@gsamokovarov No, I don't. I think so too :-)

gsamokovarov added a commit that referenced this pull request Aug 4, 2015
@gsamokovarov gsamokovarov merged commit 4e46969 into rails:master Aug 4, 2015
@sh19910711
Copy link
Author

@gsamokovarov Thanks for merging 🙇

@gsamokovarov
Copy link
Collaborator

Ref. #121

@gsamokovarov
Copy link
Collaborator

Hey @sh19910711, we seem to have forgotten to include the renamed script/ folder in version control. Can you issue a PR with it?

@sh19910711
Copy link
Author

Hey. That folder might be "extensions/script"?

@simkessy
Copy link

simkessy commented Sep 3, 2015

@simkessy Thank you. The extension can detect Web Console by a response header, so I think there is no need to set up if you see the default console. All you have to do is to install the extension (and you might need to do gem "web-console", "~>2.2.0" or something perhaps).

@sh19910711 I'm trying to follow the install instructions but I'm not sure if I'm suppose to be cloning the extension locally or in vagrant? So far I'm tried to do everything in vagrant and I get this error when trying to bundle

Bundler could not find compatible versions for gem "rack":
  In Gemfile:
    rails (>= 0) ruby depends on
      actionpack (= 5.0.0.alpha) ruby depends on
        rack (~> 2.x) ruby
Could not find gem 'rack (~> 2.x) ruby', which is required by gem 'actionpack (= 5.0.0.alpha) ruby', in

any of the sources.

@sh19910711
Copy link
Author

@simkessy Hi. It's better to clone the extension locally if you use it in Chrome on the real machine.

So far I'm tried to do everything in vagrant and I get this error when trying to bundle

I got the same error on the master branch. I think we can pass it by specifying the Gemfile of Web Console (like that bundle install --gemfile=./gemfiles/Gemfile-4-2-stable or something else) for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants