Greasy Fork

Greasy Fork is available in English.

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

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

当前为 2017-12-11 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Bing Daily Picture Download button|必应每日图片下载按钮
// @namespace   http://greasyfork.icu/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.4.1
// @grant       none
// ==/UserScript==

//定时器周期行检测今日必应图片是否加载成功
let timer = setInterval(function() {
  //获取到今日必应图片信息后添加按钮 停止周期检测
  if (getImg()) {
    const imgInfo = getImg()
    addBtn(imgInfo)

    const downloadBtn = document.querySelector('#download-btn') //下载按钮
    //用户前后切换了必应图片 图片地址和名称应该对应改变 当用户移动鼠标到图片上时进行检测
    downloadBtn.onmouseover = function() {
      const newInfo = getImg() //获取图片地址
      if (this.href != newInfo.picUrl) {
        //如果新获取的地址与下载按钮的地址不同 就更改为新地址和名字
        this.href = newInfo.picUrl
        this.download = newInfo.picName
      }
    }

    clearInterval(timer)
  }
}, 233)

//获取图片地址
function getImg() {
  // 从行内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
      }
    }
  }
  //图片地址
  picUrl = picUrl.substring(5, picUrl.length - 2)
  //图片名称
  let picName = picUrl.substring(picUrl.lastIndexOf('/') + 1, picUrl.length)
  return { picUrl, picName }
}

//添加下载按钮
function addBtn(imgInfo) {
  //在必应首页添加下载按钮
  let btn = document.createElement('a')
  let text = null

  if (navigator.language.indexOf('zh') >= 0) {
    text = document.createTextNode('下载今日必应图片')
  } else {
    text = document.createTextNode('Download Today Bing Pictures')
  }

  btn.id = 'download-btn'
  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 = imgInfo.picName
  btn.href = imgInfo.picUrl
  btn.appendChild(text)
  document.body.appendChild(btn)
}