前端截取url成图片

本文介绍了一种通过Google API将URL转换为图片的方法。利用提供的API,可以为指定的URL生成包含截图的JSON响应。示例代码展示了如何用jQuery发起请求,并处理返回的数据以显示截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

   有些小需求需要将 url 转成图片, 直接使用 google的 api即可

https://developers.google.com/speed/docs/insights/v2/reference/pagespeedapi/runpagespeed

 

Request

HTTP request

 
GET https://www.googleapis.com/pagespeedonline/v2/runPagespeed

Parameters

Parameter nameValueDescription
Required query parameters
urlstringThe URL to fetch and analyze
Optional query parameters
filter_third_party_resourcesbooleanIndicates if third party resources should be filtered out before PageSpeed analysis. (Default: false)
localestringThe locale used to localize formatted results
rulestringA PageSpeed rule to run; if none are given, all rules are run
screenshotbooleanIndicates if binary data containing a screenshot should be included (Default: false)
strategystringThe analysis strategy to use 

Acceptable values are:
  • "desktop": Fetch and analyze the URL for desktop browsers
  • "mobile": Fetch and analyze the URL for mobile devices

 

   

<!DOCTYPE html>
<html>

  <head lang="en">
    <meta charset="utf-8">
    <title>IIIII</title>
    <style>
      body {
        /*background: #BADA55;*/
      }
    </style>
    <script src="//cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>
    
  </head>

  <body>
    
    <ul>
      <img data-url="http://vb2005xu.iteye.com"/>
      <img data-url="https://www.oschina.net/p/rocket-chat"/>
      <img data-url="http://alloyteam.github.io/AlloyTimer/"/>
    </ul>
    
  </body>
<script>
    $(window).load(function() {

    $('img[data-url]').each(function() {
        $.ajax({
          url: 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=' + $(this).data('url') + '&screenshot=true&strategy=mobile',
          context: this,
          type: 'GET',
          dataType: 'json',
          success: function(data) {
              data = data.screenshot.data.replace(/_/g, '/').replace(/-/g, '+');
              $(this).attr('src', 'data:image/jpeg;base64,' + data);
            }
        });
    });


});
    </script>

</html>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值