Readability Line Gradient

Color lines to assist eye transistion similar to BeeLine Reader. Uses Lining.js for (responsive) DOM wrapping of paragraph text.

当前为 2020-03-22 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Readability Line Gradient
// @namespace    https://neekleer.tacden.net/
// @version      0.3.2
// @description  Color lines to assist eye transistion similar to BeeLine Reader. Uses Lining.js for (responsive) DOM wrapping of paragraph text.
// @author       neekleer
// @match        http*://*.storiesonline.net/s/*
// @match        http*://*.novelfull.com/*/*.html
// @match        http*://*.novelplanet.com/Novel/*/*
// @grant        GM_addStyle
// @require      https://cdn.jsdelivr.net/lining.js/0.3.3/lining.min.js
// ==/UserScript==

;(function () {
  'use strict'
  if (typeof window.lining !== 'function') return

  function getContrastYIQ (color) {
    let parsed = color.match(
      /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
    )
    let r, g, b
    r = g = b = 255
    if (parsed) {
      if (parsed.length >= 4) {
        r = parsed[1]
        g = parsed[2]
        b = parsed[3]
      }
    } else {
      parsed = color
        .replace('#', '')
        .padEnd(6, '0')
        .match(/^([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/)
      if (parsed.length >= 4) {
        r = parseInt(parsed[1], 16)
        g = parseInt(parsed[2], 16)
        b = parseInt(parsed[3], 16)
      }
    }

    return (r * 299 + g * 587 + b * 114) / 1000 < 128
  }

  function run () {
    setTimeout(() => {
      let mainContainer = document.body
      if (window.location.hostname.endsWith('novelfull.com')) {
        const container = document.getElementById('container')
        if (container) {
          mainContainer = container
        }
      } else if (window.location.hostname.endsWith('novelplanet.com')) {
        const container = document.getElementById('main')
        if (container) {
          mainContainer = container
        }
      }

      const darkMode = getContrastYIQ(
        window
          .getComputedStyle(mainContainer)
          .getPropertyValue('background-color')
      )
      const colorA = darkMode ? '#C1C1C1' : '#000'
      const colorB = darkMode ? '#FB6558' : '#0F52BA'
      const colorC = darkMode ? '#377BE6' : '#E34234'
      const elements = document.querySelectorAll('p:not(:empty)')
      for (let i = elements.length - 1; i >= 0; i--) {
        window.lining(elements[i], { autoResize: true })
      }

      const lineSpec = [
        ['3n - 2', colorA, colorA, colorB],
        ['3n - 1', colorB, colorA, colorC],
        ['3n', colorC, colorA, colorA]
      ]

      for (let i = 0, l = lineSpec.length; i < l; i++) {
        const multExpr = lineSpec[i].shift()
        const colorList = lineSpec[i]
        lineSpec[
          i
        ] = `p .line:nth-of-type(${multExpr}){background:linear-gradient(90deg,${colorList.join()});color:transparent;background-clip:text;-webkit-background-clip:text;-webkit-text-fill-color:transparent}`
        if (colorList.pop() !== colorA) {
          colorList.push(colorA)
          lineSpec[
            i
          ] += `\np .line[last]:nth-of-type(${multExpr}){background:linear-gradient(90deg,${colorList.join()});color:transparent;background-clip:text;-webkit-background-clip:text;-webkit-text-fill-color:transparent}`
        }
      }
      GM_addStyle(lineSpec.join('\n'))
    }, 1000)
  }

  run()
})()