Module: FerrumCommon

Defined in:
lib/ferrum_common.rb

Defined Under Namespace

Modules: Common, CoreGraphics

Class Method Summary collapse

Class Method Details

.browser_reposition(ferrum_browser, mtd, dx = 0, dy = 0) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ferrum_common.rb', line 99

def self.browser_reposition ferrum_browser, mtd, dx = 0, dy = 0
  case ::Etc.uname[:sysname]
  when "Darwin"
    count_ptr = ::FFI::MemoryPointer.new :uint32
    CoreGraphics.CGGetActiveDisplayList 0, nil, count_ptr
    display_ids = ::FFI::MemoryPointer.new :uint32, count_ptr.read_uint32
    CoreGraphics.CGGetActiveDisplayList count_ptr.read_uint32, display_ids, nil
    displays = display_ids.read_array_of_uint32(count_ptr.read_uint32).map do |display_id|
      CoreGraphics.CGDisplayBounds display_id
    end
    best = displays.map{ |_| _[:x] }.method(mtd).call
    display = displays.find{ |_| best == _[:x] }
    ferrum_browser.position = {left: display[:x] + dx, top: display[:y] + dy}
  else
    "#{::Etc.uname[:sysname].inspect} isn't supported yet"
  end
end

.parse_mhtml(mhtml_string) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ferrum_common.rb', line 117

def self.parse_mhtml mhtml_string
  scanner = ::StringScanner.new mhtml_string
  fail scanner.peek(400).inspect unless scanner.scan(/\AFrom: <Saved by Blink>\r
Snapshot-Content-Location: \S+\r
Subject:(?: \S.*\r\n)+Date: [A-Z][a-z][a-z], \d\d? [A-Z][a-z][a-z] 20\d\d \d\d:\d\d:\d\d [-+]0\d00\r
MIME-Version: 1\.0\r
Content-Type: multipart\/related;\r
\ttype="text\/html";\r
\tboundary="(----MultipartBoundary--[a-zA-Z0-9]{42}----)"\r\n\r\n\r\n--\1/)
  delimeter = scanner[1]
  fail unless scanner.charpos == prev = scanner.pos
  while s = scanner.search_full(::Regexp.new(delimeter), true, true)
    case doc = s[0...-delimeter.size-4]
    when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: chrome-error:\/\/chromewebdata\/\r\n\r\n/,
         /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r\n\r\n/
      # trash
    when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
      # html
    when /\A\r\nContent-Type: text\/css\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
      # css
    when /\A\r\nContent-Type: image\/(webp|png|gif|jpeg)\r
Content-Transfer-Encoding: base64\r
Content-Location: (https?:\S+)\r\n\r\n(([+\/0-9A-Za-z=]{,76}\r\n)+)\z/
      yield $1, $2, $3
    when /\A\r\nContent-Type: binary\/octet-stream\r
Content-Transfer-Encoding: base64\r
Content-Location: https:\/\/\S+\r\n\r\n/
      # binary
    when /\A\r\nContent-Type: image\/svg\+xml\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: https?:\S+\r\n\r\n/
      # svg
    when /\A\r\nContent-Type: image\/gif\r
Content-ID: <frame-[0-9A-F]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: base64\r
Content-Location: https?:\S+\r\n\r\n/
      # gif
    else
      STDERR.puts s[0..300]
      fail
    end
    fail unless scanner.charpos == prev = scanner.pos
  end
end

.process_mhtmlObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ferrum_common.rb', line 175

def self.process_mhtml
  scanner = ::StringScanner.new(mht = ARGF.read)
  fail scanner.peek(400).inspect unless scanner.scan(/\AFrom: <Saved by Blink>\r
Snapshot-Content-Location: \S+\r
Subject:(?: \S.*\r\n)+Date: [A-Z][a-z][a-z], \d\d? [A-Z][a-z][a-z] 20\d\d \d\d:\d\d:\d\d [-+]0\d00\r
MIME-Version: 1\.0\r
Content-Type: multipart\/related;\r
\ttype="text\/html";\r
\tboundary="(----MultipartBoundary--[a-zA-Z0-9]{42}----)"\r\n\r\n\r\n--\1/)
  delimeter = scanner[1]
  fail unless scanner.charpos == prev = scanner.pos
  reps = []
  while s = scanner.search_full(::Regexp.new(delimeter), true, true)
    doc = s[0...-delimeter.size-4]
    case doc
    when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: chrome-error:\/\/chromewebdata\/\r\n\r\n/,
         /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r\n\r\n/
      STDERR.puts "trash #{$'.size}"
      reps.push [prev-delimeter.size-2, scanner.pos-delimeter.size-4, "", ""]
    when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
      STDERR.puts "html #{$'.size}"
      header = $&
      t = $'.gsub(/=([0-9A-F][0-9A-F])/){ fail $1 unless "3D" == $1 || "20" == $1 || "0A" == $1 || "09" == $1 unless "80" <= $1; $1.hex.chr }.gsub("=\r\n", "")
      STDERR.puts "unpacked #{t.size}"
      html = ::Nokogiri::HTML t#.force_encoding "utf-8"

      STDERR.puts ".to_s.size #{html.to_s.size}"

      html.xpath("//*[not(*)]").group_by(&:name).
        map{ |_, g| [_, g.map(&:to_s).map(&:size).reduce(:+)] }.
        sort_by(&:last).reverse.take(5).each{ |_| STDERR.puts _.inspect }

      if block_given?
        yield html
        STDERR.puts "yielded"
        STDERR.puts "yield #{html.to_s.size}"
      end

      reps.push [prev, scanner.pos-delimeter.size-4, header, html.to_s, true, :html]
    when /\A\r\nContent-Type: text\/css\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
      STDERR.puts "css > #{$'.size}"
      header = $&
      css = $'.gsub(/=([0-9A-F][0-9A-F])/){ fail $1 unless "3D" == $1 || "20" == $1 || "0A" == $1 unless "80" <= $1; $1.hex.chr }.gsub("=\r\n", "")
      css.gsub!(/[\r\n]+/, "\n")

      STDERR.puts "css < #{css.size}"
      reps.push [prev, scanner.pos-delimeter.size-4, header, css, true, :css]

    when /\A\r\nContent-Type: image\/(webp|png|gif|jpeg)\r
Content-Transfer-Encoding: base64\r
Content-Location: https?:\S+\r\n\r\n/
      STDERR.puts "#{$1} #{$'.size}"
    when /\A\r\nContent-Type: binary\/octet-stream\r
Content-Transfer-Encoding: base64\r
Content-Location: https:\/\/\S+\r\n\r\n/
      STDERR.puts "binary #{$'.size}"
    when /\A\r\nContent-Type: image\/svg\+xml\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: https?:\S+\r\n\r\n/
      STDERR.puts "svg #{$'.size}"
    when /\A\r\nContent-Type: image\/gif\r
Content-ID: <frame-[0-9A-F]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: base64\r
Content-Location: https?:\S+\r\n\r\n/
      STDERR.puts "gif #{$'.size}"
    else
      STDERR.puts doc[0..300]
      fail
    end
    fail unless scanner.charpos == prev = scanner.pos
  end

  is = reps.map.with_index{ |(_, _, _, _, _, type), i| i if :html == type }.compact
  STDERR.puts is.inspect
  cs = reps.map.with_index{ |(_, _, _, _, _, type), i| i if :css == type }.compact
  STDERR.puts cs.inspect
  cs.each_cons(2){ |i,j| fail unless i+1==j }
  fail unless is == [cs[0]-1]
  File.write "temp.htm", reps[is[0]][3]
  File.open("temp.css", "w"){ |f| cs.each{ |i| f.puts reps[i][3] } }
  STDERR.puts "css > #{File.size "temp.css"}"
  system "uncss temp.htm -s temp.css -o out.css"
  STDERR.puts "css < #{File.size "out.css"}"
  reps[cs[0]][1] = reps[cs[-1]][1]
  reps[cs[0]+1..cs[-1]] = []
  reps[cs[0]][3] = File.read "out.css"

  reps.reverse_each do |from, to, header, str, qp|
    str = qp ?
      header + str.gsub("=", "=3D").
        b.gsub(/[\x80-\xFF]/n){ |_| "=%02X" % _.ord }.
        gsub(/.{73}[^=][^=](?=.)/, "\\0=\r\n") :
      header + str.gsub("\n", "\r\n")
    STDERR.puts [str.size, "to - from = #{to - from}"].inspect
    mht[from...to] = str
  end
  puts mht
  STDERR.puts "OK"
end