ネコでも分かる(大嘘)
ajax時代の
greasemonkeyの書き方

@kbigwheel

greasemonkey知ってますか?

javascriptを書くことにより、
既存のページをだれでも改変できるようになる
便利なブラウザプラグイン

(firefox発だがIE, chromeにも互換プラグインがある)

普段見ているページが
ちょっとだけ便利になる

実例

基本テンプレート

// ==UserScript==
// @name         適当な名前
// @namespace    https://github.com/bigwheel とか
// @version      1.0
// @description  this is description
// @author       著者
// @match        https://github.com/* 適応したいURLをワイルドカードで
// @grant        GM_xmlhttpRequest SCPやらajaxのクロスドメイン問題を避けるためにこれを使おう
// ==/UserScript==
 
if(document.body && $('ここで指定した要素があると変更を適応')) {
    MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
    var observer = new MutationObserver(function(mutations, observer) {
        // 変更したい内容
    });
    
    observer.observe(document, {
        subtree: true,
        attributes: true
    });
};

「ajaxページ用tampermonkeyテンプレート」でググればそのうち出る様になると思う

  • ポイントは2つ
    1. MutationObserverでDOMの変更自体を監視・対応
    2. GM_xmlhttpRequestでセキュリティを無視してajax通信

だれでも書けるajax時代のgreasemonkeyスクリプト

By kbigwheel

だれでも書けるajax時代のgreasemonkeyスクリプト

  • 2,772