微博看h图

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         微博看h图
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://weibo.com/fangge617/home?wvr=5
// @grant        none
// @include           http://www.weibo.com/*
// @include           http://weibo.com/*
// @include           http://d.weibo.com/*
// @include           http://s.weibo.com/*
// @include           https://www.weibo.com/*
// @include           https://weibo.com/*
// @include           https://d.weibo.com/*
// @include           https://s.weibo.com/*
// @exclude           http://weibo.com/a/bind/*
// @exclude           http://weibo.com/nguide/*
// @exclude           http://weibo.com/
// @exclude           https://weibo.com/a/bind/*
// @exclude           https://weibo.com/nguide/*
// @exclude           https://weibo.com/
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
      let canvas = document.createElement('canvas')
  let ctx = canvas.getContext('2d')

  function invertImg (originImg) {
    if (!(originImg instanceof window.Image)) {
      return
    }

    // 跨域
    let img = new window.Image()
    img.crossOrigin = 'anonymous'
    img.onerror = () => window.alert('载入图片失败,可能是跨域问题?')
    img.onload = () => {
      [canvas.width, canvas.height] = [img.width, img.height]
      ctx.drawImage(img, 0, 0)

      // 反色
      let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height)
      for (let i = 0; i < imgData.data.length; i += 4) {
        imgData.data[i] = ~imgData.data[i] & 0xFF
        imgData.data[i + 1] = ~imgData.data[i + 1] & 0xFF
        imgData.data[i + 2] = ~imgData.data[i + 2] & 0xFF
      }
      ctx.putImageData(imgData, 0, 0)
      originImg.src = canvas.toDataURL()
    }

    if (originImg.src.startsWith('data:')) {
      img.src = originImg.src
    } else {
      // 防缓存
      img.src = originImg.src + (originImg.src.indexOf('?') === -1 ? '?_t=' : '&_t=') + new Date().getTime()
    }
  }

  // 监听右键菜单
  document.addEventListener('contextmenu', event => {
    if (event.target instanceof window.Image) {
      event.preventDefault()
      invertImg(event.target)
    }
  })
})();