lib/isomorfeus/puppetmaster/driver/puppeteer_document.rb in isomorfeus-puppetmaster-0.2.9 vs lib/isomorfeus/puppetmaster/driver/puppeteer_document.rb in isomorfeus-puppetmaster-0.3.0

- old
+ new

@@ -124,15 +124,13 @@ await ("AllPageHandles[#{document.handle}].setExtraHTTPHeaders({});") end def document_clear_url_blacklist(document) await <<~JAVASCRIPT - if (!(BrowserType === 'firefox')) { - var cdp_session = await AllPageHandles[#{document.handle}].target().createCDPSession(); - await cdp_session.send('Network.setBlockedURLs', {urls: []}); - await cdp_session.detach(); - } + var cdp_session = await AllPageHandles[#{document.handle}].target().createCDPSession(); + await cdp_session.send('Network.setBlockedURLs', {urls: []}); + await cdp_session.detach(); JAVASCRIPT end def document_click(document, x: nil, y: nil, modifiers: nil) # modifier_keys: :alt, :control, :meta, :shift @@ -150,16 +148,11 @@ else { setTimeout(function(){ resolve(true); }, #{@puppeteer_timeout}); } AllPageHandles[#{document.handle}].removeListener('response', response_handler); }, #{@puppeteer_reaction_timeout}); }); AllPageHandles[#{document.handle}].on('response', response_handler); - var navigation_watcher; - if (BrowserType === 'firefox') { - navigation_watcher = AllPageHandles[#{document.handle}].waitFor(1000); - } else { - navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); - } + var navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); await AllPageHandles[#{document.handle}].evaluate(function(){ var options = {button: 0, bubbles: true, cancelable: true}; var node = window; var x = #{x ? x : 'null'}; var y = #{y ? y : 'null'}; @@ -288,16 +281,11 @@ else { setTimeout(function(){ resolve(true); }, #{@puppeteer_timeout}); } AllPageHandles[#{document.handle}].removeListener('response', response_handler); }, #{@puppeteer_reaction_timeout}); }); AllPageHandlers[#{document.handle}].on('response', response_handler); - var navigation_watcher; - if (BrowserType === 'firefox') { - navigation_watcher = AllPageHandles[#{document.handle}].waitFor(1000); - } else { - navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); - } + var navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); await AllPageHandles[#{document.handle}].evaluate(function(){ var options = {button: 0, bubbles: true, cancelable: true}; var node = window; var x = #{x ? x : 'null'}; var y = #{y ? y : 'null'}; @@ -321,19 +309,19 @@ end def document_evaluate_script(document, script, *args) await <<~JAVASCRIPT LastResult = await AllPageHandles[#{document.handle}].evaluate((arguments) => { - return #{script} + return #{script.strip} }, #{args}); JAVASCRIPT end def document_execute_script(document, script, *args) await <<~JAVASCRIPT LastResult = await AllPageHandles[#{document.handle}].evaluate((arguments) => { - #{script} + #{script.strip} }, #{args}); JAVASCRIPT end def document_find(document, selector) @@ -587,17 +575,15 @@ handle, response_hash, messages = await <<~JAVASCRIPT var new_page = await CurrentBrowser.newPage(); var url = '#{parsed_uri.to_s}'; new_page.setDefaultTimeout(#{@puppeteer_timeout}); await new_page.setViewport({width: #{@width}, height: #{@height}}); - if (!(BrowserType === 'firefox')) { - var new_target = new_page.target(); - var cdp_session = await new_target.createCDPSession(); - await cdp_session.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '#{Isomorfeus::Puppetmaster.download_path}'}); - if (#{@url_blacklist}.length > 0) { await cdp_session.send('Network.setBlockedURLs', {urls: #{@url_blacklist}}); } - await cdp_session.detach(); - } + var new_target = new_page.target(); + var cdp_session = await new_target.createCDPSession(); + await cdp_session.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '#{Isomorfeus::Puppetmaster.download_path}'}); + if (#{@url_blacklist}.length > 0) { await cdp_session.send('Network.setBlockedURLs', {urls: #{@url_blacklist}}); } + await cdp_session.detach(); var page_handle = RegisterPage(new_page); var result_response = null; if (url && url !== '') { var response = await new_page.goto(url); if (response) { @@ -675,16 +661,11 @@ else { setTimeout(function(){ resolve(true); }, #{@puppeteer_timeout}); } AllPageHandles[#{document.handle}].removeListener('response', response_handler); }, #{@puppeteer_reaction_timeout}); }); AllPageHandles[#{document.handle}].on('response', response_handler); - var navigation_watcher; - if (BrowserType === 'firefox') { - navigation_watcher = AllPageHandles[#{document.handle}].waitFor(1000); - } else { - navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); - } + var navigation_watcher = AllPageHandles[#{document.handle}].waitForNavigation(); await AllPageHandles[#{document.handle}].evaluate(function(){ var options = {button: 2, bubbles: true, cancelable: true}; var node = window; var x = #{x ? x : 'null'}; var y = #{y ? y : 'null'}; @@ -778,21 +759,19 @@ end await "await AllPageHandles[#{document.handle}].setCookie({#{final_options.join(', ')}});" end def document_set_extra_headers(document, headers_hash) - await "await AllPageHandles[#{document.handle}].setExtraHTTPHeaders({#{headers_hash.map { |k, v| "'#{k}': '#{v}'" }.join(', ')}});" + await "await AllPageHandles[#{document.handle}].setExtraHTTPHeaders(#{Oj.dump(headers_hash, mode: :strict)});" end def document_set_url_blacklist(document, url_array) # https://www.chromium.org/administrators/url-blacklist-filter-format @url_blacklist = url_array await <<~JAVASCRIPT - if (!(BrowserType === 'firefox')) { - var cdp_session = await AllPageHandles[#{document.handle}].target().createCDPSession(); - await cdp_session.send('Network.setBlockedURLs', {urls: #{url_array}}); - await cdp_session.detach(); - } + var cdp_session = await AllPageHandles[#{document.handle}].target().createCDPSession(); + await cdp_session.send('Network.setBlockedURLs', {urls: #{url_array}}); + await cdp_session.detach(); JAVASCRIPT end def document_set_user_agent(document, agent_string) await "await AllPageHandles[#{document.handle}].setUserAgent('#{agent_string}');"