简体   繁体   English

如何使用 url 输入创建一个表单,该输入重定向到 pagespeed 分数/见解或使用 ajax 显示它

[英]How to create a form with an url input that redirects to pagespeed score / insights or displays it with ajax

Is it possible to do this?是否有可能做到这一点? Ideally to return the report in the very same page with ajax?理想情况下是在与 ajax 相同的页面中返回报告? Example the user adds www.mywebsite.com to the field and then the report of pagespeed is returned.示例用户将www.mywebsite.com添加到该字段,然后返回 pagespeed 报告。 If not possible then redirect to Pagespeed result page.如果不可能,则重定向到 Pagespeed 结果页面。

You have a few options here.您在这里有几个选择。 Starting from easiest to hardest (and in my opinion "worst" to "best" solution).从最简单到最难(在我看来,从“最差”到“最好”的解决方案)。

  1. Add the Page Speed Insights (PSI) test page to an iframe on your site.将 Page Speed Insights (PSI) 测试页添加到您站点上的 iframe。 You can then change the URL of that iframe to https://developers.google.com/speed/pagespeed/insights/?url=yourwebsite.com and manipulate the ?url=yourwebsite.com to be whatever you want. You can then change the URL of that iframe to https://developers.google.com/speed/pagespeed/insights/?url=yourwebsite.com and manipulate the ?url=yourwebsite.com to be whatever you want.

This may be against Google's terms of service and is also a bad user experience but it is the easiest way to achieve it.这可能违反了 Google 的服务条款,也是一种糟糕的用户体验,但这是实现它的最简单方法。 I will leave you to investigate that option if you decide to do it.如果您决定这样做,我会让您调查该选项。


  1. Redirect users to a new tab.将用户重定向到新选项卡。 So just do <a target="_blank" href="https://developers.google.com/speed/pagespeed/insights/?url=yourwebsite.com">view your report</a> or redirect via JS on a button click.所以只需<a target="_blank" href="https://developers.google.com/speed/pagespeed/insights/?url=yourwebsite.com">view your report</a>或通过JS重定向按钮单击。

Yet again not a great option as people are leaving your site but at least this won't be against Google's terms of service.这又不是一个很好的选择,因为人们正在离开您的网站,但至少这不会违反 Google 的服务条款。


  1. Use the page speed insights API.使用页面速度洞察 API。 https://developers.google.com/speed/docs/insights/v5/get-started . https://developers.google.com/speed/docs/insights/v5/get-started

This is your best option in terms of time vs flexibility.就时间与灵活性而言,这是您的最佳选择。 You supply the API with the URL and it returns a JSON response with all of the metrics it gathers and the scoring.您向 API 提供 URL 并返回 JSON 响应以及它收集的所有指标和评分。

Please note PSI is on version 6 of the API which should be available for general use soon.请注意,PSI 位于 API 的第 6 版上,该版本应该很快就能用于一般用途。

Obviously this is a lot more work but well worth the effort as you can style everything as you please.显然,这是一项更多工作,但值得付出努力,因为您可以随意设计所有样式。


  1. Install Lighthouse, the engine that drives PSI on your own server.在您自己的服务器上安装驱动 PSI 的引擎 Lighthouse。

You can find the Lighthouse repository here .您可以在此处找到 Lighthouse 存储库 Please note you need to know how to use node, it is useful to understand puppeteer and you need a reasonable amount of server admin knowledge to get chromium (used as a headless web browser for running the tests) working and linked correctly.请注意,您需要知道如何使用节点,了解 puppeteer 很有用,并且您需要合理数量的服务器管理知识才能使 chromium(用作运行测试的无头 web 浏览器)正常工作和链接。

At this stage you have complete control and can write your own test, scoring criteria etc. You can also run as many tests as your server will allow.在这个阶段,您拥有完全的控制权,可以编写自己的测试、评分标准等。您还可以运行服务器允许的尽可能多的测试。 If you want this level of control and freedom then this is the best option.如果您想要这种级别的控制和自由,那么这是最佳选择。 However be prepared to sink a lot of hours into this solution!但是,请准备好在此解决方案中投入大量时间!

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

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