A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

180 lines
5.8 KiB

  1. function set_background_potd() {
  2. if (cache_cookie()) return;
  3. var d = new Date();
  4. var callback = "earwigpotd1";
  5. var date = (d.getUTCFullYear()) + "-" + zero_pad(d.getUTCMonth() + 1, 2) + "-" + zero_pad(d.getUTCDate(), 2);
  6. var base = "//commons.wikimedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=Template:Potd/";
  7. var url = base + date + "&callback=" + callback;
  8. var script = document.createElement("script");
  9. var head = document.getElementsByTagName("head")[0];
  10. window[callback] = function(data) {
  11. head.removeChild(script);
  12. parse_potd_file_name(data);
  13. };
  14. script.src = url;
  15. head.appendChild(script);
  16. }
  17. function set_background_list() {
  18. if (cache_cookie()) return;
  19. var base = "//commons.wikimedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=User:The+Earwig/POTD";
  20. var url = base + "&callback=" + callback;
  21. var script = document.createElement("script");
  22. var head = document.getElementsByTagName("head")[0];
  23. window[callback] = function(data) {
  24. head.removeChild(script);
  25. parse_list_file_name(data);
  26. };
  27. script.src = url;
  28. head.appendChild(script);
  29. }
  30. function cache_cookie() {
  31. var cookie = get_cookie("EarwigBackgroundCache");
  32. if (cookie) {
  33. try {
  34. data = JSON.parse(cookie);
  35. var filename = data.filename;
  36. var url = data.url;
  37. var descurl = data.descurl;
  38. var imgwidth = data.imgwidth;
  39. var imgheight = data.imgheight;
  40. if (filename && url && descurl && imgwidth && imgheight) {
  41. set_background(filename, url, descurl, imgwidth, imgheight);
  42. return true;
  43. }
  44. }
  45. catch (SyntaxError) {}
  46. }
  47. return false;
  48. }
  49. function parse_potd_file_name(data) {
  50. var content = "";
  51. var res = data["query"]["pages"];
  52. for (pageid in res) {
  53. content = res[pageid]["revisions"][0]["*"];
  54. }
  55. var filename = /\{\{Potd filename\|(1=)?(.*?)\|.*?\}\}/.exec(content)[2];
  56. var callback = "earwigpotd2";
  57. var base = "//commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=url|size&format=json&titles=File:";
  58. var url = base + escape(filename) + "&callback=" + callback;
  59. var script = document.createElement("script");
  60. var head = document.getElementsByTagName("head")[0];
  61. window[callback] = function(data) {
  62. head.removeChild(script);
  63. parse_file_url(data, escape(filename.replace(/ /g, "_")));
  64. };
  65. script.src = url;
  66. head.appendChild(script);
  67. }
  68. function parse_list_file_name(data) {
  69. var content = "";
  70. var res = data["query"]["pages"];
  71. for (pageid in res) {
  72. content = res[pageid]["revisions"][0]["*"];
  73. }
  74. var filenames = [];
  75. while ((match = /\*\s*\[\[:File:(.*?)\]\]/g.exec(content)) !== null) {
  76. filenames.push(match[1]);
  77. }
  78. var filename = filenames[Math.floor(Math.random() * filenames.length)];
  79. var callback = "earwigpotd2";
  80. var base = "//commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=url|size&format=json&titles=File:";
  81. var url = base + escape(filename) + "&callback=" + callback;
  82. var script = document.createElement("script");
  83. var head = document.getElementsByTagName("head")[0];
  84. window[callback] = function(data) {
  85. head.removeChild(script);
  86. parse_file_url(data, escape(filename.replace(/ /g, "_")));
  87. };
  88. script.src = url;
  89. head.appendChild(script);
  90. }
  91. function parse_file_url(data, filename) {
  92. var url = "";
  93. var descurl = "";
  94. var imgwidth = 1024;
  95. var imgheight = 768;
  96. var res = data["query"]["pages"];
  97. for (pageid in res) {
  98. r = res[pageid]["imageinfo"][0];
  99. url = r["url"];
  100. descurl = r["descriptionurl"];
  101. imgwidth = r["width"];
  102. imgheight = r["height"];
  103. }
  104. set_background(filename, url, descurl, imgwidth, imgheight);
  105. var data = {"filename": filename, "url": url, "descurl": descurl, "imgwidth": imgwidth, "imgheight": imgheight};
  106. var expires = new Date();
  107. expires.setUTCMilliseconds(0);
  108. expires.setUTCSeconds(0);
  109. expires.setUTCMinutes(0);
  110. expires.setUTCHours(0);
  111. expires.setUTCDate(expires.getUTCDate() + 1);
  112. set_cookie_with_date("EarwigBackgroundCache", JSON.stringify(data), expires);
  113. }
  114. function set_background(filename, url, descurl, imgwidth, imgheight) {
  115. var s = get_window_size();
  116. var winwidth = s[0];
  117. var winheight = s[1];
  118. var width = winwidth;
  119. if (imgwidth/imgheight > winwidth/winheight) {
  120. width = Math.round((imgwidth/imgheight) * winheight);
  121. }
  122. if (width >= imgwidth) {
  123. document.body.style.backgroundImage = "url('" + url + "')";
  124. if (width > imgwidth) {
  125. document.body.style.setProperty("background-size", "cover");
  126. }
  127. } else {
  128. url = url.replace(/\/commons\//, "/commons/thumb/") + "/" + width + "px-" + filename;
  129. document.body.style.backgroundImage = "url('" + url + "')";
  130. }
  131. document.getElementById("bg_image_link").href = descurl;
  132. }
  133. function zero_pad(value, length) {
  134. value = String(value);
  135. length = length || 2;
  136. while (value.length < length) {
  137. value = "0" + value;
  138. }
  139. return value;
  140. }
  141. function get_window_size() {
  142. // See http://www.javascripter.net/faq/browserw.htm
  143. if (document.body && document.body.offsetWidth && document.body.offsetHeight) {
  144. return [document.body.offsetWidth, document.body.offsetHeight];
  145. }
  146. if (document.compatMode=="CSS1Compat" && document.documentElement && document.documentElement.offsetWidth && document.documentElement.offsetHeight) {
  147. return [document.documentElement.offsetWidth, document.documentElement.offsetHeight];
  148. }
  149. if (window.innerWidth && window.innerHeight) {
  150. return [window.innerWidth, window.innerHeight];
  151. }
  152. return [1024, 768];
  153. }