コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

利用者:Camillu87/enhanced random article.js

お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。

多くの WindowsLinux のブラウザ

  • Ctrl を押しながら F5 を押す。

Mac における Safari

  • Shift を押しながら、更新ボタン をクリックする。

Mac における ChromeFirefox

  • Cmd Shift を押しながら R を押す。

詳細についてはWikipedia:キャッシュを消すをご覧ください。

// Generated by CoffeeScript 1.7.1

/*
	ENHANCED RANDOM ARTICLE
	Documentation: [[Wikipedia:Enhanced Random Article]]
	Description: Enhances the existing "Random article" feature.
		Disambiguation pages, redirects, and pages with titles that match a certain pattern can be skipped over.
	
	TODO
	- Match patterns to NOT show.
 */
var addEnhancedRandomArticlePortletLink, addPortletLink, checkRandomMatch, eRCheckMatches, eRCreateArticleList, enhancedRandomArticle, enhancedRandomArticleBegin, matchDisambig, mw;

if (typeof enhancedRandomArticle === "undefined" || enhancedRandomArticle === null) {
  enhancedRandomArticle = {};
}

if (typeof unsafeWindow !== 'undefined') {
  addPortletLink = unsafeWindow.addPortletLink;
  mw = unsafeWindow.mw;
}

enhancedRandomArticleBegin = function() {

  /*
  		DEFAULT SETTINGS
  		
  		enhancedRandomArticle = { patterns: [], showDisambiguationPages: false, showStubs: false }
  		
  		enhancedRandomArticle = {}
  		enhancedRandomArticle.patterns = []
  		enhancedRandomArticle.showDisambiguationPages = false
  		enhancedRandomArticle.showStubs = false
   */
  if (enhancedRandomArticle == null) {
    enhancedRandomArticle = {};
  }
  if (enhancedRandomArticle.patterns == null) {
    enhancedRandomArticle.patterns = [];
  }
  if (enhancedRandomArticle.showDisambiguationPages == null) {
    enhancedRandomArticle.showDisambiguationPages = false;
  }
  if (enhancedRandomArticle.showStubs == null) {
    enhancedRandomArticle.showStubs = false;
  }
  return $.get(mw.config.get('wgScriptPath') + '/api.php', {
    action: 'query',
    list: 'random',
    rnnamespace: 0,
    format: 'json',
    rnlimit: 10
  }, eRCreateArticleList);
};

eRCreateArticleList = function(obj) {
  var article, articles, articlesArray, _i, _len;
  articles = obj.query.random;
  articlesArray = [];
  for (_i = 0, _len = articles.length; _i < _len; _i++) {
    article = articles[_i];
    articlesArray.push(article.title);
  }
  return $.get(mw.config.get('wgScriptPath') + '/api.php', {
    action: 'query',
    prop: 'revisions',
    rvdir: 'older',
    rvprop: 'content',
    titles: articlesArray.join('|'),
    format: 'json'
  }, eRCheckMatches);
};

matchDisambig = function(possiblePatterns, content) {
  var matchExpr, pattern, _i, _len;
  for (_i = 0, _len = possiblePatterns.length; _i < _len; _i++) {
    pattern = possiblePatterns[_i];
    matchExpr = new RegExp(pattern, 'i');
    if (content.match(matchExpr)) {
      return true;
    }
  }
  return false;
};

checkRandomMatch = function(userSettings, title, content) {
  var disambiguationMatches, eRA, matched, matchedPattern, pattern, _i, _len, _ref;
  eRA = userSettings;
  disambiguationMatches = ['disambig}}', 'dis}}', 'disambiguation}}', 'surname}}'];
  matched = false;
  matchedPattern = '';
  _ref = eRA.patterns;
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    pattern = _ref[_i];
    if (title.match(pattern)) {
      matched = true;
      matchedPattern = pattern;
      break;
    }
  }
  if (!eRA.patterns.length && eRA.showDisambiguationPages && eRA.showStubs) {
    console.log(1);
    matched = true;
  }
  if (matched && !eRA.showDisambiguationPages && matchDisambig(disambiguationMatches, content)) {
    matched = false;
  } else if (!eRA.patterns.length && !eRA.showDisambiguationPages && !matchDisambig(disambiguationMatches, content)) {
    console.log(2);
    matched = true;
  }
  if (matched && !eRA.showStubs && content.match(/stub}}/i)) {
    matched = false;
  } else if (!eRA.patterns.length && !eRA.showStubs && !content.match(/stub}}/i)) {
    console.log(3);
    matched = true;
  }
  return matched;
};

eRCheckMatches = function(obj) {
  var article, articles, content, eRA, matched, page, title, value, _i, _len, _ref;
  articles = [];
  _ref = obj.query.pages;
  for (page in _ref) {
    value = _ref[page];
    articles.push(value);
  }
  eRA = enhancedRandomArticle;
  if (typeof eRA.patterns === 'string') {
    eRA.patterns = [eRA.patterns];
  }
  for (_i = 0, _len = articles.length; _i < _len; _i++) {
    article = articles[_i];
    content = article['revisions'][0]['*'];
    title = article['title'];
    matched = checkRandomMatch(eRA, title, content);
    if (matched) {
      window.location = '/wiki/' + title;
      return;
    }
  }
  return alert("ENHANCED RANDOM ARTICLES\n\nAn article that meets all of your criteria could not be found. Please try again.\nAlso, perhaps consider using less restrictive criteria.\n\nKeep in mind that only 10 articles are retrieved each time this script is activated,\nso there's a good chance that most of them won't match your criteria if it's too strict.");
};

addEnhancedRandomArticlePortletLink = function() {
  var node, nodeLink, randomPageLink;
  if ($('#p-navigation').length) {
    mw.util.addPortletLink('p-navigation', '#', 'Enhanced random', 't-enhanced-random-article', 'Show a random article, with enhanced settings', '', '#n-randompage');
    $('#t-enhanced-random-article').on('click', function(event) {
      event.preventDefault();
      return enhancedRandomArticleBegin();
    });
    if ($('#n-randompage').length) {
      randomPageLink = $('#n-randompage').children().eq(0);
      randomPageLink.attr('accesskey', null);
    }
    node = $('#t-enhanced-random-article');
    nodeLink = node.children().eq(0);
    nodeLink.attr('accesskey', 'x');
    return nodeLink.attr('title', nodeLink.attr('title') + ' [ctrl-x]');
  }
};

$(addEnhancedRandomArticlePortletLink);

if (typeof unsafeWindow !== 'undefined') {
  unsafeWindow.checkRandomMatch = checkRandomMatch;
  unsafeWindow.enhancedRandomArticle = enhancedRandomArticle;
  unsafeWindow.enhancedRandomArticleBegin = enhancedRandomArticleBegin;
  unsafeWindow.eRCheckMatches = eRCheckMatches;
  unsafeWindow.eRCreateArticleList = eRCreateArticleList;
  unsafeWindow.matchDisambig = matchDisambig;
}