npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auto-compute-first-screen-time

v5.7.7

Published

A tool for auto computing first screen time of one page.

Downloads

285

Readme

What is this ?

A tool for computing first screen time of one page with inaccuracy of 0 automatically.

What's the defination of first screen time ?

  • If there are images existing in first screen, the defination is:

    the time when all images in first screen loaded.
  • If there is no image existing in first screen, the defination is:

    performance.timing.domContentLoadedStart

Precision

The distance between average tested time and real first screen time is 0 (tested in wifi/fast 3G/slow 3G)

How To Use

auto-compute-first-screen-time use libaray of umd.

So, you can use it by:

  • <script src="./auto-compute-first-screen-time/dist/index.js"></script>

    var autoComputeFirstScreenTime = window.autoComputeFirstScreenTime.

  • var autoComputeFirstScreenTime = require('auto-compute-first-screen-time');

And then use it:

  • compute first screen time automatically

    Run this code before the scripts of page running.

    autoComputeFirstScreenTime({
        onReport: function (result) {
            if (result.success) {
                console.log(result.firstScreenTime)
            } else {
                console.log(result);
            }
        }
    });
    
    // other scripts of current page
    // ...
  • compute first screen time by hand when you find it ready

    autoComputeFirstScreenTime.report({
        // required: false
        onReport: function (result) {
            if (result.success) {
                console.log(result.firstScreenTime)
            } else {
                console.log(result);
            }
        }
    });
  • options

    Options means autoComputeFirstScreenTime(options) and autoComputeFirstScreenTime.report(options).

    • options.type

      Computing type, which should be one of below: auto/perf/dot. perf by default.

      We suggest you using type 'perf' because this type won't make image request at all and it covers 92+% mobile browsers.

    • options.onReport

      • type: Function

      • default

        onReport(result) {
            // blank function
        }
      • description

        It will run when first screen time is found.

        onReport(result) {
            if (result.success) {
                console.log(result.firstScreenTime)
            } else {
                console.log(result);
            }
        }
    • options.request

      • type: Object

      • description

        limitedIn: [], // RegExp as item
        exclude: [] // RegExp as item

        auto-compute-first-screen-time will catch request for computing first screen time.

        limitedIn controls which kind of requests should be caught, such as [/mtop\.alibaba\.com/i].

        exclue controls which kind of requests should not be caught, such as [/list\.alibaba\.com/i].

    • options.delayReport

      • type: Number

      • default: 0

      • description

        auto-compute-first-screen-time will run onReport callback immediately by default.

        When delayReport is setted, auto-compute-first-screen-time will run onReport after some time.

        It can be used in some pages that require users login.

        Delay report can help you avoid report the wrong page (always login page).

        For example:

        delayReport: 1000 // ms
    • options.jsonpFilter

      • type: RegExp

      • default: /jsonp=callback/

      • description

        Filter for cathing jsonp request.

    • options.navigationStartChangeTag

      • type: Array

      • default: ['data-perf-start', 'perf-start']

      • description

        Usually, when first screen time stamp is found, we get the first screen time by:

        var firstScreenTime = firstScreenTimeStamp - performance.timing.navigationStart

        But for single-page-application(SPA), it's wrong.

        Because SPA has sub routes, when route changes, the first screen time should be computed by:

        firstScreenTimeStamp - the-timestamp-when-route-changed

        auto-compute-first-screen-time will watch options.navigationStartChangeTag changes when computing first screen.

        'data-perf-start' will be watched firstly, if there is no 'data-perf-start', 'perf-start' will be watched. And the tags must be setted on <body>.

        So for SPA, you should do one more job: when route changes, reset perf-start or data-perf-start on <body>.

        For example by vue SPA:

        router.afterEach(function(to, from) {
            document.body.dataset.perfStart = Date.now();
        })
  • Dom control

    • perf-ignore

      ignore images inside the tagged dom (tagged dom included)

      <div>
          <img src="xxx" />
      </div>
      <div perf-ignore> <!-- ignored -->
          <img src="xxx" /> <!-- ignored -->
      </div>
      
      <div perf-ignore style="background: url(xxx) 0 0 no-repeat;"></div> <!-- ignored -->
    • <anytag perf-scroll></anytag>

      anytag means tags like div / span / ul / ....

      Usually, when we get images in first screen, we should firstly get node position by formula as below:

      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // changeable
      
      var boundingClientRect = imgNode.getBoundingClientRect();
      if ((scrollTop + boundingClientRect.top) < window.innerHeight && boundingClientRect.right > 0 && boundingClientRect.left < window.innerWidth) {
          console.log('this node is in first screen');
      }

      When perf-scroll is added on a tag, part of the formula will change as below:

      from

      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

      to

      var scrollTop = document.querySelector('[perf-scroll]').getBoundingClientRect().top;
      
      if (scrollWrapperClientRect.top < 0) {
          scrollTop = -scrollWrapperClientRect.top;
      } else {
          scrollTop = 0;
      }

Support xhr ?

Yes!

Support fetch ?

Yes!

Support jsonp ?

Perhaps not...

Support async js like webpack split bundle ?

Yes!

Details

details

LICENSE

BSD