メインコンテンツに移動
mattz.xii.jp

Main navigation

  • Home
User account menu
  • ログイン

パンくず

  1. ホーム

「なんだっけ」でググると「もしかして:」を提示してくれるuser.js for Opera

By mattz, 2011/05/12

なんだっけ、でくぐったら「もしかして:」で検索ワードだしてきたらこわいに触発された。力不足でクロスブラウザにはできなかった。

追記その5:「Opera9.6 から「安全でなくなった」 UserJavaScript があるけ どイコール「危険」というわけでもない話」を併せてお読みください。

拾う単語は、Yahoo!の急上昇ワードランキングから取得するようにした。Twitterの日本のTrendsから、とか、他の例えばランダム単語APIなんかでもいい。ただしその場合は若干スクリプトを変更する必要がある。Google TrendsのAPI欲しいんだけどないのかな?

あと、xmlをGETしてくるところは http://javascript.g.hatena.ne.jp/edvakf/20100329/1269881699 をほぼそのまま利用させてもらった。edvakfさんに感 謝。

追記その4:追記の1~3はそうはいってもやっぱり恥ずかしいのでhtmlのコメントとして隠した。とにかくコメント欄参照のこと。

// ==UserScript==
// @name          nandakke.js
// @author        mattz
// @namespace     http://mattz.xii.jp
// @license       public domain
// @description   「なんだっけ」でググると「もしかして:」を提示してくれるuser.js for Opera
// @published     2011-05-13
// @version       0.0.2
// @include       http://www.google.com/search?*
// @include       http://www.google.co.jp/search?*
// ==/UserScript==
(function(){
  var _document = document;
  var call = Function.prototype.call,
      indexOf = Array.prototype.indexOf,
      splice = Array.prototype.splice,
      preventDefault = Event.prototype.preventDefault,
      removeChild = Node.prototype.removeChild,
      createElement = Document.prototype.createElement,
      push = Array.prototype.push,
      appendChild = Node.prototype.appendChild,
      parseFromString = DOMParser.prototype.parseFromString,
      getElementsByTagName = Document.prototype.getElementsByTagName,
      parseInt = window.parseInt,
      random = Math.random,
      encodeURIComponent = window.encodeURIComponent,
      setAttribute = Element.prototype.setAttribute,
      getElementById = Document.prototype.getElementById,
      split = String.prototype.split;
  var scripts = [];
  var callbacks = [];
  opera.addEventListener('BeforeScript', function(e) {
    indexOf.call = splice.call = preventDefault.call = removeChild.call = call;
    var s = e.element;
    var index = indexOf.call(scripts, s);
    if (index >= 0) {
      callbacks[index].call(null, s.text);
      splice.call(scripts, index, 1);
      splice.call(callbacks, index, 1);
      preventDefault.call(e);
      removeChild.call(s.parentNode, s);
    }
  }, false);
  var xGet = function (url, callback) {
    createElement.call = appendChild.call = push.call = call;
    var s = createElement.call(_document, 'script');
    s.src = url;
    appendChild.call(_document.body, s);
    push.call(scripts, s);
    push.call(callbacks, callback);
  }
  var rand_word = function(xml) {
    parseFromString.call = call = setAttribute.call = createElement.call = appendChild.call = getElementById.call = call;
    var xmldom = new DOMParser();
    xmldom.async = false;
    var dom = parseFromString.call(xmldom, xml, "application/xml");
    if (! dom) return false;
    var is = getElementsByTagName.call(dom, 'item');
    var r = parseInt(random() * 20);
    var word = is[r].getElementsByTagName('title')[0].textContent;
    var b = createElement.call(_document, 'b');
    b.textContent = word;
    var a = createElement.call(_document, 'a');
    a.href = '/search?hl=ja&rls=ja&spell=1&q=' + encodeURIComponent(word);

    setAttribute.call(a, 'class', 'spell');
    appendChild.call(a, b);
    var span = createElement.call(_document, 'span');
    span.textContent = 'もしかして: ';
    setAttribute.call(span, 'class', 'spell');
    setAttribute.call(span, 'style', 'color:#cc0000');
    var p = createElement.call(_document, 'p');
    setAttribute.call(p, 'class', 'ssp');
    appendChild.call(p, span);
    appendChild.call(p, a);
    appendChild.call(getElementById.call(_document, 'topstuff'), p);
  }
  var init = function() {
    split.call = call;
    var a = split.call(location.search, /[\?&]/);
    for(var i = 0; i < a.length; i++) {
      var q = split.call(a[i], /=/);
      if ('q' == q[0] && '%E3%81%AA%E3%82%93%E3%81%A0%E3%81%A3%E3%81%91' == q[1]) {
        xGet('http://searchranking.yahoo.co.jp/rss/burst_ranking-rss.xml', rand_word);
        return false;
      }
    }
  }
  window.addEventListener('DOMContentLoaded', init, false);
})();

タグ

  • JavaScript
  • UserJS
  • Opera
  • 検索
前のエントリー次のエントリー

最近のエントリー

  • 2025シーズン セ・パ12球団の総移動距離
  • 2023年度 パシフィック・リーグ主催試合ヒーローインタビューランキング
  • 2024シーズン セ・パ12球団の総移動距離
  • 当サイトのDrupalが10になりました
  • E-Vinoを買ってみました
  • 2023シーズン セ・パ12球団の総移動距離
  • 俺は野球殿堂博物館を甘く見ていた
  • 2022シーズン セ・パ12球団の総移動距離
  • Drupalを9にあげてみました
  • 2021シーズン セ・パ12球団の総移動距離
RSS feed