Interaction with browsers has never been a job for the faint of heart: about half a dozen different APIs, different IPC mechanisms, and different capabilities from different providers. Projects such as WebDriver , trying to abstract from this complexity, besides on the Web you can find dozens of other "headless" drivers using WebKit or other engines. Currently, even the W3C specification on WebDriver is in the works./Applications/Path To/Google Chrome --remote-debugging-port=9222 # OSX $> curl localhost:9222/json [ { "devtoolsFrontendUrl": "/devtools/devtools.html?host=localhost:9222&page=1", "faviconUrl": "", "thumbnailUrl": "/thumb/chrome://newtab/", "title": "New Tab", "url": "chrome://newtab/", "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/1" } ] webSocketDebuggerUrl . Let's try to do something about it (translator's note: Ruby authoring code, but this is not fundamental): require 'em-http' require 'faye/websocket' require 'json' EM.run do # Chrome runs an HTTP handler listing available tabs conn = EM::HttpRequest.new('http://localhost:9222/json').get conn.callback do resp = JSON.parse(conn.response) puts "#{resp.size} available tabs, Chrome response: \n#{resp}" # connect to first tab via the WS debug URL ws = Faye::WebSocket::Client.new(resp.first['webSocketDebuggerUrl']) ws.onopen = lambda do |event| # once connected, enable network tracking ws.send JSON.dump({id: 1, method: 'Network.enable'}) # tell Chrome to navigate to twitter.com and look for "chrome" tweets ws.send JSON.dump({ id: 2, method: 'Page.navigate', params: {url: 'http://twitter.com/#!/search/chrome?q=chrome&' + rand(100).to_s} }) end ws.onmessage = lambda do |event| # print event notifications from Chrome to the console p [:new_message, JSON.parse(event.data)] end end end Network.responseReceived event ). In fact, if you leave the Web page open, you will also see long polling events generated for new tweets. Tons of information , all at your disposal.Source: https://habr.com/ru/post/141981/
All Articles