104月/090
GreaseMonkeyにようやく手を付けてみた
さわろうさわろうと思って、手を付けずじまいだったGreaseMonkeyにようやく手を付けてみました。
いやー、便利ですねー。失敗したなぁ...。
とりあえず、下のエントリを見ながらほぼコピペでサイトのcssをまるっと差し替えるスクリプトを書いてみました。
// ==UserScript==
// @name asahi.com css replace
// @namespace http://www.s-cut.net/
// @description replace css files
// @include http://www.asahi.com/*
// ==/UserScript==
function replaceCssPath(xpath, csspath){
var style = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
style.setAttribute('href', csspath);
}
/* インラインスタイルシートの場合はこちら */
function replaceStyle(xpath, text){
var style = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
style.firstChild.nodeValue = text;
}
(function(){
// 置換したいstyle要素のxpath
var xpath01 = '/html/head/link';
var xpath02 = '/html/head/link[2]';
var xpath03 = '/html/head/link[3]';
var xpath04 = '/html/head/link[4]';
// 置換したいcssのパス
var csspath01 = 'http://www.s-cut.net/stylish/asahi.com/common.css';
// 置換
replaceCssPath(xpath01, csspath01);
replaceCssPath(xpath02, '');
replaceCssPath(xpath03, '');
replaceCssPath(xpath04, '');
}
)();
これは、asahi.comのcssを自分で書いたcssに置換するものです。
YUIのreset.css当ててスッピンにしてみました。
簡単に検証とかサンプル作りに使えるので、デザイナーでも知っておいた方がいいですね。
いい玩具です。
