Wallhaven - Hide AI art from main page

Hide wallpapers with AI tag from the main page

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Wallhaven - Hide AI art from main page
// @match       https://wallhaven.cc/
// @grant       GM.getValue
// @grant       GM_fetch
// @grant       GM.setValue
// @run-at      document-end
// @version     1.0.1
// @author      TheQwertiest
// @description Hide wallpapers with AI tag from the main page
// @license MIT
// @namespace https://greasyfork.org/users/1302515
// ==/UserScript==


function removeAiArt(apiKey) {
  for(const cl of ['sm-thumb', 'lg-thumb']) {
    let elements = document.getElementsByClassName(cl);
    for (let el of elements) {
      const image_url = el.getElementsByTagName('a')[0].href;
      const image_id = image_url.split('/').slice(-1);

      GM_fetch(`https://wallhaven.cc/api/v1/w/${image_id}?apikey=${apiKey}`, {
        method: 'GET'
      }).then(function(response){
        let responseJson = JSON.parse(response.text);
        const aiTag = responseJson.data.tags.find((tag) => tag.id === 133451);
        if (aiTag) {
          this.style.display = 'none';
        }
      }.bind(el));
    }
  }
}

async function getApiKey() {
  let apiKey = await GM.getValue('API_KEY', '');
  if (!apiKey) {
    apiKey = prompt('This UserScript requires Wallhaven API key.\nYou can get it from your account page - https://wallhaven.cc/settings/account');
    if (apiKey) {
      await GM.setValue('API_KEY', apiKey);
    }
  }

  return apiKey;
}

(async() => {
  let apiKey = await getApiKey();
  if (apiKey) {
    removeAiArt(apiKey);
  }
})();