ferrum-har 0.1.2 → 0.1.4

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: 7d8d40ce71b743bf429c007041d4e1df81c0cf82b9765d8cf886249abf882543
4
- data.tar.gz: 33c293040e516c1e28fc1ad31c95a8c6e43ed9509b2b773644efcfe0eca74d2a
3
+ metadata.gz: ece6969fb3a3135a534510dbbee3a94544dfceaaebbf7ffdf2ca3b7ef9483a0c
4
+ data.tar.gz: f1feb7448c4b4cd86b823e22f433f650bcbe7f2bae13b68668d35efaebc427a9
5
5
  SHA512:
6
- metadata.gz: 6fa0a91fefc4e7a6c14b176257521e9c02941d420e6ad64e0e5681e1efd3b6121b97c33d685338176a0c8c2381213f994f6989fb92ae55f8e44ad68d99bffe4d
7
- data.tar.gz: 2e93582b28d0c0ceb59638f3e92c0e3ef74178a27ba6d20d1a08b5578d55e091fefc709d14557abe07824cd778c05d8a97b278d79fd4ab9e62ac763842477f05
6
+ metadata.gz: 6f60149634aababca262378cf044e8ffdd9c4dd0f6e34556ce336d3f50aec819ff43a7aea21cac26e30fa8cb7b820cb964d7a9ae9538d22c38c2a1c2d38582b2
7
+ data.tar.gz: 21cdba221d68c0ae8eebb2ab4d915b33f17d4c9d4058110101a94729ece0bd95080e99d5ddd55943c6ff604e3c31901199c6d54e0c11172d1d8460241c4169e0
data/README.md CHANGED
@@ -27,6 +27,7 @@ test run. This is mandatory to obtain the HAR from Chrome.
27
27
  browser = Ferrum::Browser.new
28
28
  page = browser.create_page
29
29
  page.go_to("https://www.bbc.co.uk")
30
+ page.network.wait_for_idle
30
31
 
31
32
  # Returns the HAR as a JSON string
32
33
  puts page.har
@@ -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,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module is prepended to Ferrum::Browser::Command to check options required by ferrum-har
4
+ module Ferrum
5
+ module Har
6
+ class ConfigurationError < RuntimeError
7
+ end
8
+
9
+ module BrowserCommandExtension
10
+ def merge_options
11
+ super.tap do |options|
12
+ check_har_related_browser_options(options)
13
+ end
14
+ end
15
+
16
+ private def check_har_related_browser_options(options)
17
+ options = options.transform_keys(&:to_s)
18
+
19
+ check_disable_extensions_option(options)
20
+ check_load_extension_option(options)
21
+ check_auto_open_option(options)
22
+ check_headless_option(options)
23
+ end
24
+
25
+ private def check_disable_extensions_option(options)
26
+ disable_extensions_option_value = options.key?("disable-extensions")
27
+ return unless disable_extensions_option_value
28
+
29
+ raise Ferrum::Har::ConfigurationError, <<~MSG
30
+ The ferrum-har gem requires the 'disable-extensions' browser option to be absent,
31
+ ie: extensions should be allowed. The gem will do this for you automatically, but if you
32
+ override the value, then ferrum-har will not work.
33
+
34
+ It was found to be set to '#{disable_extensions_option_value}'.
35
+ MSG
36
+ end
37
+
38
+ private def check_load_extension_option(options)
39
+ load_extension_option_value = options["load-extension"]
40
+ return unless load_extension_option_value != "#{Ferrum::Har::GEM_DIR}/extension"
41
+
42
+ raise Ferrum::Har::ConfigurationError, <<~MSG
43
+ The ferrum-har gem requires the 'load-extension' browser option to be set to
44
+ '#{Ferrum::Har::GEM_DIR}/extension'. It will do this for you automatically, but if you
45
+ override the value, then ferrum-har will not work.
46
+
47
+ It was found to be set to '#{load_extension_option_value}'.
48
+ MSG
49
+ end
50
+
51
+ private def check_auto_open_option(options)
52
+ auto_open_value = options["auto-open-devtools-for-tabs"]
53
+ return if auto_open_value.nil?
54
+
55
+ raise Ferrum::Har::ConfigurationError, <<~MSG
56
+ The ferrum-har gem requires the 'auto-open-devtools-for-tabs' browser option be enabled,
57
+ '#{Ferrum::Har::GEM_DIR}/extension'. It will do this for you automatically, but if you
58
+ override the value, then ferrum-har will not work.
59
+ ie: set to nil.
60
+
61
+ It was found to be set to '#{auto_open_value}'.
62
+ MSG
63
+ end
64
+
65
+ private def check_headless_option(options)
66
+ headless_value = options["headless"]
67
+ return unless options.key?("headless") && [false, nil].include?(headless_value)
68
+
69
+ raise Ferrum::Har::ConfigurationError, <<~MSG
70
+ If you are using ferrum headless, the ferrum-har gem requires the browser
71
+ option 'headless' to be set to 'new' instead of true or nil. Read this for more details:
72
+ https://developer.chrome.com/docs/chromium/new-headless
73
+
74
+ It was found to be set to '#{headless_value}'.
75
+ MSG
76
+ end
77
+ end
78
+ end
79
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ferrum
4
4
  module Har
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.4"
6
6
  end
7
7
  end
data/lib/ferrum/har.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "ferrum"
4
4
  require "ferrum/browser/options/chrome"
5
5
  require "ferrum/har/version"
6
+ require "ferrum/har/command_extension"
6
7
  require "ferrum/har/page_extension"
7
8
  require "ferrum/har/options_extension"
8
9
 
@@ -17,3 +18,6 @@ Ferrum::Page.prepend(Ferrum::Har::PageExtension)
17
18
 
18
19
  # Add the #merge_default method to Ferrum::Browser::Options::Chrome
19
20
  Ferrum::Browser::Options::Chrome.prepend(Ferrum::Har::OptionsExtension)
21
+
22
+ # Add the #check_har_related_browser_options method to Ferrum::Browser::Command
23
+ Ferrum::Browser::Command.prepend(Ferrum::Har::BrowserCommandExtension)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum-har
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Lascelles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-22 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -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/command_extension.rb
50
56
  - lib/ferrum/har/options_extension.rb
51
57
  - lib/ferrum/har/page_extension.rb
52
58
  - lib/ferrum/har/version.rb