必应首页背景图片下载

在必应首页右下角添加一个背景图片下载按钮

  1. // ==UserScript==
  2. // @name Download Bing Background Image - bing.com
  3. // @name:zh-CN 必应首页背景图片下载
  4. // @description Add download button to Bing homepage.
  5. // @description:zh-CN 在必应首页右下角添加一个背景图片下载按钮
  6. // @namespace Violentmonkey Scripts
  7. // @match https://*.bing.com/
  8. // @grant none
  9. // @version 0.9
  10. // @author Alvin
  11. // @license GNU GPLv3
  12. // @run-at document-idle
  13. // ==/UserScript==
  14.  
  15. function fileDownload(url, filename) {
  16. getBlob(url, function (blob) {
  17. saveAs(blob, filename)
  18. })
  19. }
  20. function getBlob(url, cb) {
  21. var xhr = new XMLHttpRequest()
  22. xhr.open('GET', url, true)
  23. xhr.responseType = 'blob'
  24. xhr.onload = function () {
  25. if (xhr.status === 200) {
  26. cb(xhr.response)
  27. }
  28. }
  29. xhr.send()
  30. }
  31. function saveAs(blob, filename) {
  32. if (window.navigator.msSaveOrOpenBlob) {
  33. navigator.msSaveBlob(blob, filename)
  34. } else {
  35. var link = document.createElement('a')
  36. var body = document.querySelector('body')
  37.  
  38. link.href = window.URL.createObjectURL(blob)
  39. link.download = filename
  40.  
  41. // fix Firefox
  42. link.style.display = 'none'
  43. body.appendChild(link)
  44.  
  45. link.click()
  46. body.removeChild(link)
  47.  
  48. window.URL.revokeObjectURL(link.href)
  49. }
  50. }
  51.  
  52. var parent = document.getElementById('sh_rdiv')
  53. if (parent) {
  54. parent.innerHTML += `<a role="button" id="downBtn" title="Download image" aria-label="Download image" href="" h="ID=SERP,5054.1" style="cursor: pointer;">
  55. <div class="sc_light" style="visibility: visible;">
  56. <div id="sh_lt" class="hpcDown"></div>
  57. </div>
  58. </a>`
  59.  
  60. var btn = document.getElementById('downBtn')
  61. btn.addEventListener(
  62. 'click',
  63. function () {
  64. var title =
  65. document.getElementById('sh_cp').title ||
  66. document.getElementById('musCardImageTitle').innerHTML +
  67. ' ' +
  68. document.getElementById('musCardCopyright').innerHTML
  69. var bg = document.getElementById('bgDiv').style.backgroundImage
  70. var imgUrl = bg.slice(4, -1).replace(/"/g, '')
  71. var fileName = title + '.jpg'
  72. fileDownload(imgUrl, fileName)
  73. },
  74. false
  75. )
  76. } else {
  77. var styleElem = document.head.appendChild(document.createElement('style'))
  78. styleElem.innerHTML =
  79. '#downBtn::after {display: none;} .footer {width: 96% !important; padding: 0 2% !important;}'
  80.  
  81. setTimeout(function () {
  82. var parentEl = document.getElementsByClassName('headline')[0]
  83. var parent = document.createElement('div')
  84. var el = parentEl.appendChild(parent)
  85.  
  86. el.innerHTML = `
  87. <a role="button" id="downBtn" title="Download image" aria-label="Download image" href="javascript:void(0)" style="cursor: pointer; width: 2.5rem; height: 2.5rem; position: relative;">
  88. <svg class="downloadIcon" x="0px" y="0px" viewBox="0 0 22 22" enable-background="new 0 0 22 22" aria-hidden="true" role="presentation">
  89. <path d="M17.842 11.483l-6.671 6.725-6.671-6.725.967-.967 5.017 5.049v-15.565h1.375v15.565l5.017-5.049.966.967zm-12.859 10.517v-1.375h12.375v1.375h-12.375z"></path>
  90. </svg>
  91. </a>`
  92. var btn = document.getElementById('downBtn')
  93. btn.addEventListener(
  94. 'click',
  95. function () {
  96. var card = document.getElementsByClassName('musCardCont')[0]
  97. var title = card.getElementsByClassName('title')[0].innerText
  98. var copyright = card.getElementsByClassName('copyright')[0].innerText
  99. var bg = (
  100. document.getElementsByClassName('img_uhd')[0] ||
  101. document.getElementsByClassName('img_cont')[0]
  102. ).style.backgroundImage
  103.  
  104. var imgUrl = bg.slice(4, -1).replace(/"/g, '')
  105. var fileName = title + ' ' + copyright + '.jpg'
  106. fileDownload(imgUrl, fileName)
  107. },
  108. false
  109. )
  110. }, 1000)
  111. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址