ferrum-har 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46ed855d025e19faa838772982f3b4e4bf6be389750445d91ff45a240e17da2b
4
- data.tar.gz: 5604f75db7e5fe01ea8d406377c5325ddf59761eef503ebd8e72f581b6cd0017
3
+ metadata.gz: 140d6f93cceb0ee84d05e449aa1fb5a8680a562002850a6ac6e8aeb475505f12
4
+ data.tar.gz: 14dbb30ee107b7562e3167c44d8cd6460bb5d52a7dd1856caa849dd2e4c1b0f6
5
5
  SHA512:
6
- metadata.gz: d16fa7164a200c6eecfd06f51f751f335c38222c95077a3a89ca6923557decab9d4a174846c160b36539be22c42651c024194699a7f861816f13badaec3d37bb
7
- data.tar.gz: 0522dec550b943209ea34edf0991915cd90f23f382e64a8a593eede11f2dfa0fc806534831224e7ca63911654ab95d29acf7751495b238f1f740b81673d2f1c6
6
+ metadata.gz: a157840990fdadd2b2dc4c42a9e0464f3b2ef1206da1c3cbd8c7f703d7e4ce5e309298eb25c43818907099fccb4d92329506fa56399f21edcc1e5f9980769b5c
7
+ data.tar.gz: dc280c46aa7285b6dbbaa0f11af62a7279e6d4272b9320171d06da3a1059950f307b5b6d294677d75206eac95f46278480062f0f7f2044e3aced3040ecc3ca71
data/README.md CHANGED
@@ -18,16 +18,13 @@ gem "ferrum-har"
18
18
  ## Usage
19
19
 
20
20
  Use [ferrum](https://github.com/rubycdp/ferrum) as normal and call the `har` method on
21
- the `page` (or `browser`) object. You will have to use some of the required browser options
22
- by using the `Ferrum::Har::REQUIRED_BROWSER_OPTIONS` constant.
21
+ the `page` (or `browser`) object. Chrome must be used as the browser engine.
23
22
 
24
- Note, the devtools window in Chrome will be opened for the duration of the ferrum test run.
25
- This is mandatory to obtain the HAR from Chrome.
23
+ Note, the devtools window in Chrome will be opened by ferrum-har for the duration of the ferrum
24
+ test run. This is mandatory to obtain the HAR from Chrome.
26
25
 
27
26
  ```ruby
28
- browser = Ferrum::Browser.new(
29
- browser_options: Ferrum::Har::REQUIRED_BROWSER_OPTIONS,
30
- )
27
+ browser = Ferrum::Browser.new
31
28
  page = browser.create_page
32
29
  page.go_to("https://www.bbc.co.uk")
33
30
 
@@ -53,7 +50,7 @@ One ramification of this is that the Chrome devtools must be open for the extens
53
50
 
54
51
  To get the extension to be loaded we must pass some switches to the Chrome process via ferrum,
55
52
  specifically the `--auto-open-devtools-for-tabs` and `--load-extension` switches. This is done
56
- by the `Ferrum::Har::REQUIRED_BROWSER_OPTIONS` constant.
53
+ by ferrum-har automatically.
57
54
 
58
55
  For further reading, a full list of Chrome switches can be found
59
56
  [here](https://peter.sh/experiments/chromium-command-line-switches/).
@@ -0,0 +1,10 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <!-- The only way to get the HAR is to add a chrome extension that can be loaded into the
5
+ devtools window where chrome.devtools.network.getHAR can be called. -->
6
+ <script src="devtools.js"></script>
7
+ </head>
8
+ <body>
9
+ </body>
10
+ </html>
@@ -0,0 +1,41 @@
1
+ const createHarResponse = function() {
2
+ // Ask the network panel for the HAR data. Note, we must wrap this in a "log" key to make it
3
+ // a valid HAR.
4
+ chrome.devtools.network.getHAR(
5
+ function(harLog) {
6
+ // Pass the HAR back to the inspected window. We need to base64 encode it to avoid any quoting
7
+ // and parsing issues.
8
+ const base64EncodedHar = btoa('{"log":' + JSON.stringify(harLog) + '}');
9
+ const comms = 'document.ferrumHar = "' + base64EncodedHar + '";';
10
+ chrome.devtools.inspectedWindow.eval(comms);
11
+ });
12
+ };
13
+
14
+ /**
15
+ * If the HAR is requested, create it and send it back to the inspected window.
16
+ */
17
+ const checkForHarInstruction = function() {
18
+ chrome.devtools.inspectedWindow.eval(
19
+ "document.ferrumHarRequested === true;",
20
+ function() {
21
+ // Prepare to receive another request.
22
+ chrome.devtools.inspectedWindow.eval("document.ferrumHarRequested = null;");
23
+ createHarResponse();
24
+ }
25
+ );
26
+ };
27
+
28
+ /**
29
+ * Start a continuous loop to check for the "create HAR" instruction from Ferrum. This is passed in
30
+ * by setting a JS variable in the inspected window. When this is detected, the HAR is created.
31
+ */
32
+ const loop_speed = 100;
33
+ const loop = function() {
34
+ checkForHarInstruction();
35
+ setTimeout(loop, loop_speed);
36
+ };
37
+ setTimeout(loop, loop_speed);
38
+
39
+ // Add the new chrome devtools panel. We need to do this or the extension cannot access the HAR.
40
+ // null is the image path (not needed).
41
+ chrome.devtools.panels.create("ferrum-har", null, "panel.html");
@@ -0,0 +1,12 @@
1
+ {
2
+ "manifest_version": 3,
3
+ "name": "ferrum-har extension",
4
+ "description": "A chrome extension that programmatically captures the HAR and returns it to the user",
5
+ "version": "1.0",
6
+ "permissions": [
7
+ "tabs"
8
+ ],
9
+ "host_permissions": ["<all_urls>"],
10
+ "devtools_page": "devtools.html",
11
+ "action": {}
12
+ }
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <script src="panel.js"></script>
4
+ </head>
5
+ <body>
6
+ <h2>ferrum-har</h2>
7
+ </body>
8
+ </html>
@@ -0,0 +1 @@
1
+ console.log("ferrum-har panel.js loaded");
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module is prepended to Ferrum::Browser::Options::Chrome to alter the default options so they
4
+ # work with the ferrum-har gem.
5
+ module Ferrum
6
+ module Har
7
+ module OptionsExtension
8
+ def merge_default(flags, options)
9
+ super
10
+ # We have to remove the "disable-extensions" option from the default browser extension
11
+ # list because the ferrum-har gem requires an (internal) extension to work.
12
+ .except("disable-extensions")
13
+ .merge(
14
+ "auto-open-devtools-for-tabs" => nil, # Chrome devtools must be open to invoke getHAR
15
+ "load-extension" => "#{Ferrum::Har::GEM_DIR}/extension", # Extension that gets the HAR
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ferrum
4
4
  module Har
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
data/lib/ferrum/har.rb CHANGED
@@ -1,23 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "ferrum"
4
+ require "ferrum/browser/options/chrome"
4
5
  require "ferrum/har/version"
5
6
  require "ferrum/har/page_extension"
7
+ require "ferrum/har/options_extension"
6
8
 
7
9
  module Ferrum
8
10
  module Har
9
11
  GEM_DIR = File.expand_path("../..", __dir__)
10
- REQUIRED_BROWSER_OPTIONS = {
11
- "auto-open-devtools-for-tabs" => nil, # The chrome devtools must be open to invoke getHAR
12
- "load-extension" => "#{Ferrum::Har::GEM_DIR}/extension", # The extension that gets the HAR
13
- }.freeze
14
12
  end
15
13
  end
16
14
 
17
- # We have to remove the "disable-extensions" option from the default browser extension list because
18
- # the ferrum-har gem requires an (internal) extension to work.
19
- Ferrum::Browser::Options::Chrome::DEFAULT_OPTIONS =
20
- Ferrum::Browser::Options::Chrome::DEFAULT_OPTIONS.except("disable-extensions")
21
-
22
15
  # Add the #har method to Ferrum::Page
23
16
  Ferrum::Page.prepend(Ferrum::Har::PageExtension)
17
+
18
+ # Add the #merge_default method to Ferrum::Browser::Options::Chrome
19
+ Ferrum::Browser::Options::Chrome.prepend(Ferrum::Har::OptionsExtension)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum-har
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Lascelles
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2024-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ferrum
14
+ name: base64
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: base64
28
+ name: ferrum
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -46,7 +46,13 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - README.md
49
+ - extension/devtools.html
50
+ - extension/devtools.js
51
+ - extension/manifest.json
52
+ - extension/panel.html
53
+ - extension/panel.js
49
54
  - lib/ferrum/har.rb
55
+ - lib/ferrum/har/options_extension.rb
50
56
  - lib/ferrum/har/page_extension.rb
51
57
  - lib/ferrum/har/version.rb
52
58
  homepage: https://github.com/hlascelles/ferrum-har