Bing Daily Pictures Download button|必应每日图片下载按钮

Add a button for downloading bing-daily-pictures.添加一个必应每日图片下载按钮。

目前為 2017-11-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Bing Daily Pictures Download button|必应每日图片下载按钮
// @namespace   https://greasyfork.org/en/users/131965-levinit
// @author      levinit
// @description Add a button for downloading bing-daily-pictures.添加一个必应每日图片下载按钮。
// @include     *://cn.bing.com/
// @include     *://www.bing.com/
// @include     *://www.bing.com/?*
// @include     *://cn.bing.com/?*
// @run-at      document-start
// @version     0.1.1
// @grant       none
// ==/UserScript==
window.onload = function() {
  // 从行内css属性background-image中获取今日必应图片的url()
  let picUrl = document.querySelector("#bgDiv").style.backgroundImage;

  //如果css属性background-image写在外部css或者style标签中
  if (picUrl === "") {
    let style0 = document.styleSheets[0];
    let styles = style0.cssRules.length;
    for (let i = 0; i < styles; i++) {
      if (style0.cssRules[i].selectorText === "#bgDiv") {
        picUrl = style0.cssRules[i].style.backgroundImage;
      }
    }
  }

  //今日必应图片的名称
  let picName = picUrl.substring(
    picUrl.lastIndexOf("/") + 1,
    picUrl.length - 2
  );

  //今日必应图片的url
  picUrl = picUrl.substring(5, picUrl.length - 2);

  //在必应首页添加下载按钮
  let btn = document.createElement("a");
  let text = null;
  if (navigator.language === "zh") {
    text = document.createTextNode("下载今日必应图片");
  } else {
    text = document.createTextNode("Download Today Bing Pictures");
  }
  btn.style.cssText =
    "display:inline-block;padding:0.25em;border-radius:0.25em;position:fixed;z-index:1000;right:20%;top:12%;background-color:#c3d1cf94;font-size: 1.5em;";
  btn.download = picName;
  btn.href = picUrl;
  btn.appendChild(text);
  document.body.appendChild(btn);
};