Skip to content

Instantly share code, notes, and snippets.

@Hunlongyu
Last active August 17, 2021 08:47
Show Gist options
  • Save Hunlongyu/a504a27efaf18391a6af0b7c83796726 to your computer and use it in GitHub Desktop.
Save Hunlongyu/a504a27efaf18391a6af0b7c83796726 to your computer and use it in GitHub Desktop.
一键下载必应首页的图片。
// ==UserScript==
// @author Hunlongyu
// @name 『小助手』 必应图片下载
// @namespace https://github.com/Hunlongyu
// @icon https://i.loli.net/2019/04/22/5cbd720718fdb.png
// @description 一键下载必应图片。
// @version 0.2.0
// @include *://www.bing.com/*
// @run-at document-idle
// @supportURL https://gist.github.com/Hunlongyu/a504a27efaf18391a6af0b7c83796726
// note 2021/08/17 支持最新必应网站
// ==/UserScript==
(function() {
'use strict'
const Bing = function () {}
Bing.prototype = {
init: function () {
const timer = setInterval(() => {
const dom = document.querySelector('.hly_download')
if (dom) {
clearInterval(timer)
} else {
this.create()
}
}, 1000)
},
create: function () {
let _this = this
let parentDom = document.querySelector('.headline')
let dom = document.createElement('div')
dom.innerHTML = `
<a class="hly_download">
<div class="icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11 5C11 4.44772 11.4477 4 12 4C12.5523 4 13 4.44772 13 5V12.1578L16.2428 8.91501L17.657 10.3292L12.0001 15.9861L6.34326 10.3292L7.75748 8.91501L11 12.1575V5Z" fill="#ffffff" /><path d="M4 14H6V18H18V14H20V18C20 19.1046 19.1046 20 18 20H6C4.89543 20 4 19.1046 4 18V14Z" fill="#ffffff" /></svg>
</div>
</a>
`
dom.setAttribute('class', 'icon_text')
dom.setAttribute('role', 'button')
dom.setAttribute('title', '点击该按钮即可下载图片')
dom.setAttribute('aria-label', '点击该按钮即可下载图片')
parentDom.appendChild(dom)
console.log('『小助手』 已启动!并成功创建下载按钮!')
dom.addEventListener('click', function () {
_this.getImg()
})
return false
},
getImg: function () {
let root = window.location.origin
let bgDiv = document.querySelector('.img_cont').style.backgroundImage
let img = root + bgDiv.replace('url("','').replace('")','')
console.log('『小助手』 获取图片成功!地址为: ', img)
let imgInfo = document.querySelector('#copyright').innerText
this.download(img, imgInfo)
return false
},
download: function (i, ii) {
let format = i.match(/(.jpg|.png)/)[0]
let dom = document.querySelector('.hly_download')
dom.setAttribute('href', i)
dom.setAttribute('download', ii+`${format}`)
console.log('『小助手』 开始下载图片!如下载失败,请截图反馈: ', i, ii, format)
return false
}
}
let bing = new Bing()
bing.init()
})()
@Hunlongyu
Copy link
Author

大神你好,bing上不显示下载按钮了,不知是否可以修正下 Thanks♪(・ω・)ノ

hello 目前已经修复,适配最新的必应网站。切不再区分国内版和国际版。
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment