Site Notice

hello, world

Difference between revisions of "User:机智的小鱼君/common.js"

From Project-EPB Commons
([InPageEdit] 没有编辑摘要)
([InPageEdit] 没有编辑摘要)
Line 1: Line 1:
 
mw.loader.load('https://common.wjghj.cn/js/InPageEdit-v2');
 
mw.loader.load('https://common.wjghj.cn/js/InPageEdit-v2');
  
function getArrItem(arr, num) {
+
 
  var temp_array = new Array();
+
function getRandomArrayElements(arr, count) {
  for (var index in arr) {
+
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    temp_array.push(arr[index]);
+
     while (i-- > min) {
  }
+
        index = Math.floor((i + 1) * Math.random());
  var return_array = new Array();
+
        temp = shuffled[index];
  for (var i = 0; i < num; i++) {
+
        shuffled[index] = shuffled[i];
     if (temp_array.length > 0) {
+
        shuffled[i] = temp;
      var arrIndex = Math.floor(Math.random() * temp_array.length);
 
      return_array[i] = temp_array[arrIndex];
 
      temp_array.splice(arrIndex, 1);
 
    } else {
 
      break;
 
 
     }
 
     }
  }
+
    return shuffled.slice(min);
  return return_array;
 
 
}
 
}
 +
 
function randomBg() {
 
function randomBg() {
 
   new mw.Api().get({
 
   new mw.Api().get({
Line 27: Line 22:
 
     var wikitext = data.parse.wikitext['*'],
 
     var wikitext = data.parse.wikitext['*'],
 
     json = JSON.parse(wikitext),
 
     json = JSON.parse(wikitext),
     // item = getArrItem(json.list,1),
+
     item = getRandomArrayElements(json.list,1),
     item = json.list[0],
+
     // item = json.list[0],
 
     img = item.img,
 
     img = item.img,
 
     des = item.des;
 
     des = item.des;

Revision as of 12:28, 8 November 2019

mw.loader.load('https://common.wjghj.cn/js/InPageEdit-v2');


function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i];
        shuffled[i] = temp;
    }
    return shuffled.slice(min);
}

function randomBg() {
  new mw.Api().get({
    action: 'parse',
    page: 'MediaWiki:Custom-RandomBackground/List',
    prop: 'wikitext',
    format: 'json'
  }).done(function(data) {
    var wikitext = data.parse.wikitext['*'],
    json = JSON.parse(wikitext),
    item = getRandomArrayElements(json.list,1),
    // item = json.list[0],
    img = item.img,
    des = item.des;
 
    $('#firstHeading').after('<br>image url: url(' + img + ')<br>Description: '+des);
 
  });
}
$(function(){
randomBg();
$('#firstHeading').click(randomBg)
});