下载所有棋谱

下载所有棋谱 新浪围棋

  1. // ==UserScript==
  2. // @name Get sgf files
  3. // @name:zh-CN 下载所有棋谱
  4. // @name:zh-TW 下載所有sgf文件
  5. // @namespace http://tampermonkey.net/
  6. // @version 0.2
  7. // @description Retrieve all sgf files with one click of a button.
  8. // @description:zh-cn 下载所有棋谱 新浪围棋
  9. // @description:zh-tw 下載所有sgf文件 新浪围棋
  10. // @author Qiang Li
  11. // @match http://sinago.com/gibo/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. console.log('i am running');
  18.  
  19. //
  20. var hrefs = new Array();
  21. var els = document.querySelectorAll(' a[href*="sgf"]')
  22. var elsarray = [...els]
  23. let title1;
  24. let title2;
  25. let title3;
  26. var elsarray2 = [];
  27. elsarray.forEach((xx, index) => {
  28. // get the url
  29. let s = xx.href.indexOf("http");
  30. let e = xx.href.indexOf("sgf");
  31. let yy = xx.href.substring(s, e + 3);
  32. // get the text
  33.  
  34. if (index % 3 === 0){
  35. title1 = xx.innerText;
  36. }else if (index % 3 === 1){
  37. title2 = xx.innerText;
  38. }else {
  39.  
  40. title3 = xx.innerText;
  41. //console.log('title1 ' + title1+',title2 '+title2 +', title3 '+title3 )
  42. // we can create object {title, url}
  43. elsarray2.push({name: `${title1}黑-${title2}白-${title3}.sgf`,url:yy});
  44. }
  45. });
  46. //console.log(elsarray2);
  47. // fetch the first one
  48. async function fetchsgf(url) {
  49. const res = await fetch(url);
  50. const sgf = await res.text();
  51. return sgf;
  52. }
  53.  
  54. // counter
  55. var counter = 0;
  56. function download(content, fileName, contentType) {
  57. console.log(counter++ + '. saving '+fileName);
  58. var a = document.createElement("a");
  59. var file = new Blob([content], {type: contentType});
  60. a.href = URL.createObjectURL(file);
  61. a.download = fileName;
  62. a.click();
  63. }
  64.  
  65. function downloadAll(){
  66. console.log("downloadall");
  67. //
  68. elsarray2.forEach((o, index) => {
  69. setTimeout(() =>
  70. fetch(o.url)
  71. .then(response => response.text())
  72. .then(data => download(data, index+"."+o.name, 'text/plain'))
  73. .catch(error => console.log(error)),index*200)
  74. });
  75. }
  76. function fetch_retry(url, options, n) {
  77. return fetch(url, options).catch(function(error) {
  78. console.log('error occured');
  79. console.error(error);
  80. if (n === 1) throw error;
  81. return fetch_retry(url, options, n - 1);
  82. });
  83. }
  84. AddYT();
  85. //download(jsonData, 'json.txt', 'text/plain');
  86. //fetch(elsarray2[0].url)
  87. // .then(response => response.text())
  88. // .then(data => download(data, elsarray2[0].name, 'text/plain'));
  89.  
  90. function AddYT() {
  91. var buttonDiv = document.createElement("td");
  92. buttonDiv.id = "punisher";
  93. buttonDiv.style.width = "80";
  94. var addButton = document.createElement("a");
  95. addButton.appendChild(document.createTextNode("下载棋谱"));
  96. addButton.style.width = "100%";
  97. addButton.style.cursor = "pointer";
  98. addButton.style.height = "inherit";
  99. addButton.style.backgroundColor ="red";
  100. addButton.style.color = "white";
  101.  
  102. addButton.style.border = "0";
  103. addButton.style.borderRadius = "2px";
  104. addButton.style.fontSize = "1rem";
  105. addButton.style.fontFamily = "inherit";
  106. addButton.style.textAlign = "center";
  107. addButton.style.textDecoration = "none";
  108. addButton.onclick = downloadAll;
  109. buttonDiv.appendChild(addButton);
  110. var targetElement = document.querySelectorAll("select");
  111. var tp = targetElement[0].parentNode.parentNode;
  112.  
  113. tp.appendChild(buttonDiv);
  114.  
  115. }
  116. })();