typecho博客使用网站预加载instantclick脚本 浏览器控制台报错Uncaught SyntaxError: Unexpected token <

什么是instantclick预加载脚本?

  instantclick是一个预加载js文件,当用户鼠标停留在文章页面xx毫秒(时间可以定义)之后,开始自动加载,从而缩短用户放开网页时间,这就是预加载脚本。

如何这个预加载脚本?

  1.使用国外主机的朋友,只需要将下面这行代码加入在</body>之前即可!

<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>

  2.服务器在国内的朋友,只需将以下js上传到自己服务器,并在</body>之前调用即可! 

instantclick js文件

/*! instant.page v1.2.2 - (C) 2019 Alexandre Dieulot - https://instant.page/license */

let urlToPreload
let mouseoverTimer
let lastTouchTimestamp

const prefetcher = document.createElement('link')
const isSupported = prefetcher.relList && prefetcher.relList.supports && prefetcher.relList.supports('prefetch')
const isDataSaverEnabled = navigator.connection && navigator.connection.saveData
const allowQueryString = 'instantAllowQueryString' in document.body.dataset
const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset

if (isSupported && !isDataSaverEnabled) {
  prefetcher.rel = 'prefetch'
  document.head.appendChild(prefetcher)

  const eventListenersOptions = {
    capture: true,
    passive: true,
  }
  document.addEventListener('touchstart', touchstartListener, eventListenersOptions)
  document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
}

function touchstartListener(event) {
  /* Chrome on Android calls mouseover before touchcancel so `lastTouchTimestamp`
   * must be assigned on touchstart to be measured on mouseover. */
  lastTouchTimestamp = performance.now()

  const linkElement = event.target.closest('a')

  if (!isPreloadable(linkElement)) {
    return
  }

  linkElement.addEventListener('touchcancel', touchendAndTouchcancelListener, {passive: true})
  linkElement.addEventListener('touchend', touchendAndTouchcancelListener, {passive: true})

  urlToPreload = linkElement.href
  preload(linkElement.href)
}

function touchendAndTouchcancelListener() {
  urlToPreload = undefined
  stopPreloading()
}

function mouseoverListener(event) {
  if (performance.now() - lastTouchTimestamp < 1100) {
    return
  }

  const linkElement = event.target.closest('a')

  if (!isPreloadable(linkElement)) {
    return
  }

  linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})

  urlToPreload = linkElement.href

  mouseoverTimer = setTimeout(() => {
    preload(linkElement.href)
    mouseoverTimer = undefined
  }, 65)
}

function mouseoutListener(event) {
  if (event.relatedTarget && event.target.closest('a') == event.relatedTarget.closest('a')) {
    return
  }

  if (mouseoverTimer) {
    clearTimeout(mouseoverTimer)
    mouseoverTimer = undefined
  }
  else {
    urlToPreload = undefined
    stopPreloading()
  }
}

function isPreloadable(linkElement) {
  if (!linkElement || !linkElement.href) {
    return
  }

  if (urlToPreload == linkElement.href) {
    return
  }

  const preloadLocation = new URL(linkElement.href)

  if (!allowExternalLinks && preloadLocation.origin != location.origin && !('instant' in linkElement.dataset)) {
    return
  }

  if (!['http:', 'https:'].includes(preloadLocation.protocol)) {
    return
  }

  if (preloadLocation.protocol == 'http:' && location.protocol == 'https:') {
    return
  }

  if (!allowQueryString && preloadLocation.search && !('instant' in linkElement.dataset)) {
    return
  }

  if (preloadLocation.hash && preloadLocation.pathname + preloadLocation.search == location.pathname + location.search) {
    return
  }

  if ('noInstant' in linkElement.dataset) {
    return
  }

  return true
}

function preload(url) {
  prefetcher.href = url
}

function stopPreloading() {
  prefetcher.removeAttribute('href')
}

调用代码

<script src="`存放路径`/instantclick-1.2.2.js" type="module"></script>

或者使用左岸大佬的

<script src="https://cdn.zrahh.com/js/instantclick-1.2.2.js" type="module"></script>

或者用我的也是又拍云CDN

<!-- instantclick网站预加载脚本 -->
<script src="https://image.liufw.cn/irom/js/instantclick-1.2.2.js" type="module"></script>

预加载脚本instantclick 报错

  预加载脚本报错,浏览器控制台出现

Uncaught SyntaxError: Unexpected token

Uncaught SyntaxError: Unexpected token &lt;
  我的typecho博客handsome主题,使用预加载instantclick js浏览器控制台出错。一开始以为是标签的问题,后来在才发现是因为我在handesome主题设置里面将加载脚本放在了自定义 JavaScript里面导致出现错误 。
  - instant.page 是渐进式增强 - 对不支持它的浏览器没有影响。

  handsome主题正确的应该放在自定义输出body 尾部的HTML代码,或者主题文件footer.php文件</body>标签前面。

因为我的懒惰,放在模版设置里面可以避免由于handesome主题升级又要重新添加的问题,才饶了这么大的弯路,真是庸人自扰之!

网站预加载js脚本 来源于左岸博客https://www.zrahh.com/archives/399.html

互联网卡免费申请上线啦! 来领一张手机卡吧!☛☛☛点我免费领卡☚☚☚
Last modification:March 30th, 2019 at 03:11 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment

5 comments

  1. 她与233     Android /    Google Chrome

    一个人除非自己有信心,否则带给别人信心。

    1. 留方     Windows 10 /    Google Chrome
      @她与233

      测试完

  2. 留方     Windows 10 /    Google Chrome

    88888

  3. 留方     Windows 10 /    Google Chrome

    233

  4. 萧瑟     Android /    Google Chrome

    平安是幸,知足是福,清心是禄,寡欲是寿。