简体   繁体   English

如何以编程方式从网络选项卡捕获查询字符串参数

[英]How to capture query string parameters from network tab programmatically

I am trying to capture query string parameters for analytics purpose using javascript. 我正在尝试使用javascript捕获查询字符串参数以进行分析。 I did some searching and found that BMP can be used to do it but i am unable to find ample examples to implement. 我做了一些搜索,发现BMP可以用来做,但我找不到足够的例子来实现。 Could anyone point me in the right direction. 任何人都可以指出我正确的方向。

EDIT 1: I used below code using browsermob-proxy to get har file but i get ERROR: browsermob-proxy returned error when i run it . 编辑1:我使用下面的代码使用browsermob-proxy来获取har文件,但是我得到ERROR: browsermob-proxy returned error当我运行它时, ERROR: browsermob-proxy returned error I use selenium with it. 我用硒。

getHarFile() {
        const proxy = browsermb.Proxy;
        const pr = new proxy({host:"0.0.0.0",port:4444});
        pr.doHAR("http://www.cnn.com/", (err,data) => {
            if (err) {
                logger.debug('ERROR: ' + err);
            } else {
                fs.writeFileSync('ua.com.har', data, 'utf8');
                logger.debug("#HAR CREATED#");
            }
        })
    }

在此输入图像描述

My suggestion is to create your personal extension for Google Chrome, and developing an extension you can access few more apis that are not available by default in the console. 我的建议是为Google Chrome创建个人扩展程序,并开发一个扩展程序,您可以访问控制台中默认情况下不可用的其他api。 For example you will have this object in order to inspect the network tab: 例如,您将拥有此对象以检查网络选项卡:

chrome.devtools.network

Here two links you may find useful: 这里有两个有用的链接:

https://developer.chrome.com/extensions/devtools https://developer.chrome.com/extensions/devtools

https://developer.chrome.com/extensions/devtools_network https://developer.chrome.com/extensions/devtools_network

I hope it helps 我希望它有所帮助

So since I´m not quite sure of your scope I will throw you some ideas: 因为我不太确定你的范围,我会给你一些想法:

1. Fixing browsermob-proxy 1.修复browsermob-proxy

You should change the host and proxy of the browsermob-proxy. 您应该更改browsermob-proxy的主机和代理。 Change the host to 127.0.0.1 and the port with any random number (4444 its ok). 将主机更改为127.0.0.1并使用任意随机数更改端口(4444确定)。 Then, make sure your browser run in that host and proxy by changing the browser settings. 然后,通过更改浏览器设置,确保您的浏览器在该主机和代理中运行。

2. Using plain javascript 2.使用普通的javascript

2.1 Get current page query string 2.1获取当前页面查询字符串

You can get the query string using location.search . 您可以使用location.search获取查询字符串。 If you are using some BDD framework with selenium, it is possible to execute javascript code and retrieve the result. 如果您正在使用一些带有selenium的BDD框架,则可以执行javascript代码并检索结果。 You should always add a return to your code in order to recieve the response in your BDD test. 您应该始终在代码中添加一个return ,以便在BDD测试中收到响应。

2.2 Using Performance API 2.2使用Performance API

You can access to all the network information within performance api . 您可以访问性能API中的所有网络信息。 If you need to get the current page url you can use the following code: 如果您需要获取当前页面URL,可以使用以下代码:

performance.getEntriesByType("navigation")

This will return all the current navigation events and information. 这将返回所有当前导航事件和信息。

If you want to get some information of the calls that the page made you can access it using: 如果您想获得该页面所做的调用的一些信息,您可以使用以下方式访问它:

performance.getEntriesByType("resource")

This will return all the calls made by your site. 这将返回您网站的所有通话。 You have to loop over it searching the resource you want to find. 您必须循环搜索要查找的资源。


In all the ways, there is no way to get the value and key of query string as in the network tab. 在所有方面,都无法像在网络选项卡中那样获取查询字符串的值和键。 You have to separate it manually with a function, you can use the code provided here to get the value of a key. 您必须使用函数手动分隔它,您可以使用此处提供的代码来获取密钥的值。

I was finally able to do it using the s object available on chrome console. 我终于能够使用chrome控制台上提供的s对象来完成它。 The url with encoded query string was available as s.rb object in chrome console. 带有编码查询字符串的url在chrome控制台中可用作s.rb对象。 I just decoded it and extracted the query string parameters. 我刚解码它并提取了查询字符串参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM