bilibili右键简单下载所点击图片(支持所有位置的点击)

右键点击图片以下载(支持头像下载,番剧封面下载,视频封面下载,专栏图片下载,直播封面下载,甚至还有首页和空间上那一横条的下载)

  1. // ==UserScript==
  2. // @name bilibili右键简单下载所点击图片(支持所有位置的点击)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7.8
  5. // @description 右键点击图片以下载(支持头像下载,番剧封面下载,视频封面下载,专栏图片下载,直播封面下载,甚至还有首页和空间上那一横条的下载)
  6. // @author Derek Chen
  7. // @match *://www.bilibili.com/*
  8. // @include *://www.bilibili.com/video/av*
  9. // @include *://www.bilibili.com/read/cv*
  10. // @include *://t.bilibili.com/*
  11. // @include *://space.bilibili.com/*
  12. // @include *://www.bilibili.com/*
  13. // @include *://h.bilibili.com/*
  14. // @include *://game.bilibili.com/*
  15. // @include *://live.bilibili.com/*
  16. // @include *://search.bilibili.com/*
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. let header = 'https:',
  21. acceptable_classes = ['user-head c-pointer', 'notice-img c-pointer', 'img-content', 'live-up-img', 'card-1', 'card-3'],
  22. final_url = "",
  23. header_test = new RegExp(/http*/),
  24. prompt = document.createElement("div"),
  25. first_hid = true,
  26. up_name_final = "",
  27. img_list = [],
  28. detail_ = {
  29.  
  30. };
  31.  
  32.  
  33. initiate_pop();
  34.  
  35. function initiate_pop() {
  36. prompt.setAttribute('id', 'pop');
  37. prompt.setAttribute('style', `
  38. width:${window.innerWidth / 4}px;
  39. min-height:100px;
  40. position:fixed;
  41. top: -500px;
  42. left:${window.innerWidth / 3}px;
  43. border:solid 1px gray;
  44. border-radius:10px;
  45. background: rgb(237,237,237);
  46. z-index:99999;
  47. transition-duration: 500ms;
  48. max-height:500px;
  49. overflow :auto;
  50. `);
  51. prompt.innerHTML = `
  52. <h2>你想下载那张图片?</h2>
  53. `
  54. document.body.appendChild(prompt);
  55. }
  56.  
  57. function make_img_list_prompt(arr,u_arr) {
  58. let done = [],
  59. current_index = -1;
  60. document.getElementById('pop').innerHTML = "<h2>你想下载那张图片?</h2>";
  61. for (let i = 0; i < arr.length; i++) {
  62. let finished = false;
  63. for (let j = 0; j < done.length; j++) {
  64. if (arr[i] === done[j]) {
  65. finished = true;
  66. }
  67. }
  68. if (!finished && arr[i] !== '') {
  69. let img = new Image(),
  70. div = document.createElement('div'),
  71. name = document.createElement('p');
  72. current_index = i;
  73. name.innerText = u_arr[current_index];
  74. div.style.display = 'inline-block';
  75. div.setAttribute('class','pop_container')
  76. div.style.textAlign = 'center';
  77. div.appendChild(img);
  78. div.appendChild(name);
  79. img.setAttribute('class', 'pop_img')
  80. img.src = arr[i];
  81. img.style.width = `${window.innerWidth/8.5}px`;
  82. img.style.marginLeft = `${window.innerWidth/350/3}px`;
  83. document.getElementById('pop').appendChild(div);
  84. done.push(arr[i]);
  85. }
  86. }
  87. document.getElementById('pop').style.top = '0px';
  88. console.log(arr,u_arr)
  89. }
  90.  
  91. function get_all_img() {
  92. let f_url_arr = [],
  93. u_name_arr = [],
  94. all_img = document.getElementsByTagName('img');
  95. for(let a = 0; a < all_img.length;a++){
  96. f_url_arr.push(all_img[a].src.split('@')[0].split('"')[0]);
  97. u_name_arr.push(get_name(all_img[a]));
  98. }
  99. for (let i = 0; i < acceptable_classes.length; i++) {
  100. let t = document.getElementsByClassName(acceptable_classes[i]);
  101. for (let j = 0; j < t.length; j++) {
  102. if (t[j].nodeName === 'IMG') {
  103. u_name_arr.push(get_name(t[j]));
  104. f_url_arr.push(t[j].src.split('@')[0].split('"')[0]);
  105. } else {
  106. if (header_test.test(t[j].style.backgroundImage)) {
  107. u_name_arr.push(get_name(t[j]));
  108. f_url_arr.push(t[j].style.backgroundImage.replace('url("', '').split('@')[0].split('"')[0]);
  109. } else {
  110. u_name_arr.push(get_name(t[j]));
  111. f_url_arr.push(t[j].style.backgroundImage.replace('url("', header).split('@')[0].split('"')[0]);
  112. }
  113. }
  114. }
  115. }
  116. make_img_list_prompt(f_url_arr,u_name_arr);
  117. // console.log(f_url_arr);
  118. }
  119.  
  120. function check_identical(link) {
  121. if (img_list.length !== 0) {
  122. for (let i = 0; i < img_list.length; i++) {
  123. if (link === img_list[i]) {
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130.  
  131. function download(url = final_url) {
  132. console.log(final_url);
  133. if (check_identical(url.url)) {
  134. alert("已经下载过这张图片了!");
  135. return;
  136. }
  137.  
  138. if(!detail_[url.name]){
  139. detail_[url.name] = 1;
  140. }else{
  141. detail_[url.name] += 1;
  142. }
  143. try{
  144. current_img_type = url.url.substring(url.url.lastIndexOf("."));
  145. img_list.push(url.url);
  146.  
  147. fetch(url.url).then(res => res.blob()).then(blob => { //创建临时a标签以下载图片
  148.  
  149. let a = document.createElement('a');
  150.  
  151. a.style.display = 'none';
  152. a.href = URL.createObjectURL(blob);
  153. a.download = `${url.name}${detail_[url.name] + current_img_type}`;
  154. document.body.appendChild(a);
  155. a.click();
  156. document.body.removeChild(a);
  157. link = '';
  158. });
  159. }catch(e){/*nobody cares*/};
  160. }
  161.  
  162. function get_url(target) {
  163. if (target.nodeName === 'IMG') {
  164. final_url = {
  165. url: target.src.split('@')[0].split('"')[0],
  166. name: get_name(target)
  167. };
  168. return true;
  169. } else {
  170. for (let i = 0; i < acceptable_classes.length; i++) {
  171. if (target.classList.value === acceptable_classes[i]) {
  172. if (target.nodeName !== 'IMG') {
  173. final_url = {
  174. url: header_test.test(target.style.backgroundImage) ?
  175. target.style.backgroundImage.replace('url("', '').split('@')[0].split('"')[0] :
  176. target.style.backgroundImage.replace('url("', header).split('@')[0].split('"')[0],
  177. name: get_name(target)
  178. };
  179. return true;
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186. document.oncontextmenu = (e) => {
  187. console.log(e,'right click');
  188. let target = e.target,
  189. find_element = false;
  190.  
  191. if (target.nodeName === 'IMG') {
  192. final_url = {
  193. url: target.src.split('@')[0].split('"')[0],
  194. name: get_name(target)
  195. };
  196. // console.log(final_url);
  197. download();
  198. return;
  199. }
  200.  
  201. find_element = get_url(target);
  202. if (!find_element) {
  203. get_all_img();
  204. }
  205. download();
  206. }
  207.  
  208. function get_name(ele){
  209. let name = "";
  210. while (ele) {
  211. if(ele.classList){
  212. switch(ele.classList.value){
  213. case 'pop_img':
  214. name = ele.parentNode.innerText;
  215. break;
  216. case 'post-content repost':
  217. if(ele.getElementsByClassName('original-poster')[0]){
  218. name = ele.getElementsByClassName('original-poster')[0].innerText.split('@')[1].split(':')[0];
  219. }else{
  220. name = ele.getElementsByClassName('username d-i-block up-info-name')[0].innerText;
  221. }
  222. break;
  223. case 'main-content':
  224. case 'card':
  225. name = ele.getElementsByClassName('user-name fs-16 ls-0 d-i-block')[0].innerText;
  226. break;
  227. case 'live-panel-item live-up':
  228. name = ele.getElementsByClassName('live-up-name tc-dark-slate fs-14 ls-0')[0].innerText;
  229. break;
  230. case 'list-item reply-wrap ':
  231. name = ele.getElementsByClassName('name')[0].innerText;
  232. break;
  233. case 'card-box':
  234. name = ele.getElementsByClassName('count up')[0].innerText;
  235. break;
  236. default:
  237. null; // still, nobody cares
  238. }
  239. }
  240. ele = ele.parentNode;
  241. }
  242. return name;
  243. }
  244.  
  245. document.addEventListener('mouseup',(e)=>{
  246. console.log(e)
  247. if (e.target.classList.value === 'pop_img' || e.target.classList.value === 'pop_container' || e.target.id === 'pop' ) {
  248. // console.log('a')
  249. document.getElementById('pop').style.top = '0px';
  250. first_hid = false;
  251. return;
  252. }
  253. if (first_hid) {
  254. document.getElementById('pop').style.top = '-510px';
  255. first_hid = !first_hid;
  256. } else {
  257. document.getElementById('pop').style.top = '-460px';
  258. first_hid = !first_hid;
  259. }
  260. })

QingJ © 2025

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