瀏覽代碼

Custom delay on hover

Alexandre Dieulot 5 年之前
父節點
當前提交
eef07e1622
共有 4 個文件被更改,包括 20 次插入1 次删除
  1. 9 1
      instantpage.js
  2. 6 0
      test/app.js
  3. 1 0
      test/footer.html
  4. 4 0
      test/header.html

+ 9 - 1
instantpage.js

@@ -11,6 +11,14 @@ const allowQueryString = 'instantAllowQueryString' in document.body.dataset
 const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset
 const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset
 const useWhitelist = 'instantWhitelist' in document.body.dataset
 const useWhitelist = 'instantWhitelist' in document.body.dataset
 
 
+let delayOnHover = 65
+if ('instantIntensity' in document.body.dataset) {
+  const milliseconds = parseInt(document.body.dataset.instantIntensity)
+  if (milliseconds != NaN) {
+    delayOnHover = milliseconds
+  }
+}
+
 if (isSupported && !isDataSaverEnabled) {
 if (isSupported && !isDataSaverEnabled) {
   prefetcher.rel = 'prefetch'
   prefetcher.rel = 'prefetch'
   document.head.appendChild(prefetcher)
   document.head.appendChild(prefetcher)
@@ -64,7 +72,7 @@ function mouseoverListener(event) {
   mouseoverTimer = setTimeout(() => {
   mouseoverTimer = setTimeout(() => {
     preload(linkElement.href)
     preload(linkElement.href)
     mouseoverTimer = undefined
     mouseoverTimer = undefined
-  }, 65)
+  }, delayOnHover)
 }
 }
 
 
 function mouseoutListener(event) {
 function mouseoutListener(event) {

+ 6 - 0
test/app.js

@@ -15,6 +15,7 @@ let DATA_INSTANT = 0
 let SLEEP_TIME = 200
 let SLEEP_TIME = 200
 let CACHE_MAX_AGE = 0
 let CACHE_MAX_AGE = 0
 let USE_WHITELIST = 0
 let USE_WHITELIST = 0
+let HOVER_DELAY = 65
 
 
 function handleCookies(req) {
 function handleCookies(req) {
   const cookies = req.headers.cookie
   const cookies = req.headers.cookie
@@ -35,6 +36,7 @@ function handleCookies(req) {
     SLEEP_TIME = cookieValueSplit[1]
     SLEEP_TIME = cookieValueSplit[1]
     CACHE_MAX_AGE = cookieValueSplit[2]
     CACHE_MAX_AGE = cookieValueSplit[2]
     USE_WHITELIST = cookieValueSplit[3]
     USE_WHITELIST = cookieValueSplit[3]
+    HOVER_DELAY = cookieValueSplit[4]
   })
   })
 }
 }
 
 
@@ -81,12 +83,16 @@ async function requestListener(req, res) {
     if (USE_WHITELIST) {
     if (USE_WHITELIST) {
       content = content.replace('<body', '<body data-instant-whitelist')
       content = content.replace('<body', '<body data-instant-whitelist')
     }
     }
+    if (HOVER_DELAY != 65) {
+      content = content.replace('<body', `<body data-instant-intensity="${HOVER_DELAY}"`)
+    }
     dataInstantAttribute = DATA_INSTANT || USE_WHITELIST ? `data-instant` : ``
     dataInstantAttribute = DATA_INSTANT || USE_WHITELIST ? `data-instant` : ``
 
 
     content = content.replace(':checked_aqsael', DATA_INSTANT ? 'checked' : '')
     content = content.replace(':checked_aqsael', DATA_INSTANT ? 'checked' : '')
     content = content.replace(':checked_whitelist', USE_WHITELIST ? 'checked' : '')
     content = content.replace(':checked_whitelist', USE_WHITELIST ? 'checked' : '')
     content = content.replace(':value_sleep', `value="${SLEEP_TIME}"`)
     content = content.replace(':value_sleep', `value="${SLEEP_TIME}"`)
     content = content.replace(':value_cacheAge', `value="${CACHE_MAX_AGE}"`)
     content = content.replace(':value_cacheAge', `value="${CACHE_MAX_AGE}"`)
+    content = content.replace(':value_hoverDelay', `value="${HOVER_DELAY}"`)
 
 
     content += `<h1>Page ${page}</h1>`
     content += `<h1>Page ${page}</h1>`
     for (let i = 1; i <= 3; i++) {
     for (let i = 1; i <= 3; i++) {

+ 1 - 0
test/footer.html

@@ -29,6 +29,7 @@ form.addEventListener('submit', (e) => {
     form.sleep.value,
     form.sleep.value,
     form.cacheAge.value,
     form.cacheAge.value,
     form.whitelist.checked ? 1 : 0,
     form.whitelist.checked ? 1 : 0,
+    form.hoverDelay.value,
   ].join(',')
   ].join(',')
   document.cookie = `instantpage_test=${cookieValue}`
   document.cookie = `instantpage_test=${cookieValue}`
   location.reload()
   location.reload()

+ 4 - 0
test/header.html

@@ -65,6 +65,10 @@ form input[type="number"] {
     Sleep time:
     Sleep time:
     <input name="sleep" type="number" :value_sleep>
     <input name="sleep" type="number" :value_sleep>
   </label>
   </label>
+  <label>
+    Delay on hover:
+    <input name="hoverDelay" type="number" :value_hoverDelay>
+  </label>
   <label>
   <label>
     Cache max age (in seconds):
     Cache max age (in seconds):
     <input name="cacheAge" type="number" :value_cacheAge>
     <input name="cacheAge" type="number" :value_cacheAge>