Wallpaperscraft downloader

1-Click download on WallpapersCraft. You should select the resolution before downloading.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Wallpaperscraft downloader
// @name:vi         Wallpaperscraft downloader
// @namespace       http://baivong.github.io/
// @version         2.1.7
// @description     1-Click download on WallpapersCraft. You should select the resolution before downloading.
// @description:vi  Tải hình nền từ WallpapersCraft chỉ với 1-Click. Bạn có thể chọn độ phân giải ảnh trước khi tải.
// @icon            http://i.imgur.com/NA96TWE.png
// @author          Zzbaivong
// @oujs:author     baivong
// @license         MIT; https://baivong.mit-license.org/license.txt
// @include         https://wallpaperscraft.com/*
// @exclude         https://wallpaperscraft.com/wallpaper/*
// @exclude         https://wallpaperscraft.com/download/*
// @require         https://unpkg.com/[email protected]/dist/FileSaver.min.js
// @require         https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?v=a834d46
// @noframes
// @connect         self
// @supportURL      https://github.com/lelinhtinh/Userscript/issues
// @run-at          document-idle
// @grant           GM.xmlHttpRequest
// @grant           GM_xmlhttpRequest
// @grant           GM.openInTab
// @grant           GM_openInTab
// ==/UserScript==

(function() {
  'use strict';

  /**
   * Resolution config
   * @type {String} // Eg: 1920x1200, 1366x768, 1280x1024, ...
   */
  var resolution = '';

  // Do not change the code below this line, unless you know how.
  var list = document.querySelectorAll('.wallpapers__link');
  if (!/\d+x\d+/.test(resolution)) resolution = screen.width + 'x' + screen.height;

  [].forEach.call(list, function(link) {
    var info = link.querySelector('.wallpapers__info'),
      infoContent = info.innerHTML,
      img = link.querySelector('img'),
      imgName,
      res;

    res = !info ? resolution : info.textContent.match(/\d+x\d+/)[0];

    if (!img) return;
    img = img.src.replace(/\d+x\d+/, res);
    imgName = img.replace(/.*\//, '');

    link.addEventListener('click', function(e) {
      e.preventDefault();
      info.innerHTML = 'Downloading...';

      GM.xmlHttpRequest({
        method: 'GET',
        url: img,
        responseType: 'blob',
        onprogress: function(e) {
          var percent = Math.round((e.loaded / e.total) * 100);
          info.innerHTML = percent + ' %';
        },
        onload: function(response) {
          if (response.status !== 200) {
            info.innerHTML = '<span style="color:red">' + res + ' resolution is not available</span>';
            return;
          }

          var blob = response.response;

          info.innerHTML = infoContent;
          link.setAttribute('href', URL.createObjectURL(blob));
          link.setAttribute('download', imgName);

          saveAs(blob, imgName);
        },
        onerror: function(err) {
          info.innerHTML = '<span style="color:red">' + err.message + '</span>';
          console.error(err);
        },
      });
    });

    link.addEventListener('contextmenu', function(e) {
      e.preventDefault();
      GM.openInTab(img);
    });

    link.setAttribute('title', 'Click to download this image\nRight Click to open in new tab');
  });
})();