FrontPage


Rev.9を表示中。最新版はこちら

IEでサイト・ドメイン別ユーザースタイルシートの適用を可能にする。
最初の目的は、HTMLヘルプ(.chmファイル)のフォントサイズが、IEの表示フォントに連動して変化するのを抑止すること。
副効果としてサイト別のユーザースクリプトも可能になっている。
(update 2011-11-17)
/*
* Using domain specific "user style sheet" in Internet Explorer.
* This is the way of set font-size to user's choice and enable 
* [Ctrl] + [MouseWheel] zooming in compiled html help file(.chm).
* Still more effect, this enables tiny user scripts in Internet 
* Explorer(but not compatible Grease Monkey).
*
*/
HTML {
  behavior:expression( (function(el){
    var cssfile;
    var func;
    var _doc = el.document;
    var _protocol = _doc.location.protocol.toLowerCase();
    var _href = _doc.location.href.toLowerCase();

    var myFolder = 'file://C:/Documents and Settings/username/My Documents/css/';

    switch (_protocol){

      /* CSS Set for Compiled HTML Help */
      case 'mk:':
      case 'ms-its:':
      case 'ms-help:':
        cssfile = 'chm.css';
        break;

      /* CSS set for Web */
      case 'http:':
        var _domain = _doc.domain.toLowerCase();
        switch(true){
          case /.*.example.com$/.test(_domain):
          case /.*.example.net$/.test(_domain):
            cssfile = 'example.css';
            break; 

          case /.*.example.org$/.test(_domain):
            func=function(){
              var _doc = document;
              /* write user scripts in hear */
            };
            break;

          default:
            break;
        }
    }

    /* Script operation raise error in HTML component file */
    if(_href.slice(-4) != '.htc'){
      if(cssfile){
        var elmLink = _doc.createElement('link');
        elmLink.rel='stylesheet';
        elmLink.type='text/css';
        elmLink.href= myFolder + cssfile;
        _doc.getElementsByTagName('head')[0].appendChild(elmLink);
      }

      if(func){
        _doc.onreadystatechange = function(){
          if(_doc.readyState == 'complete')func();
        }
      }

    }

    /* stop the CSS expression from being endlessly evaluated */
    el.runtimeStyle.behavior = 'none';

  })(this));
}

javascript の case 文にはワイルドカードor正規表現が使えないのでswitch(true)というワザで case 文に正規表現が使える。サブドメインは正規表現で吸収する。複数ドメインに同じ設定をする場合は、case文を続けて書く。ただし、正規表現中に \ エスケープを入れると behavior としての実行に失敗する。何故?
マイコンピューターゾーンにあたると
ユーザースタイルシートを https サイトに適用したい場合は、myFolder がイントラネットゾーンまたはインターネットゾーンとして評価されるようにする。フォルダ共有とuncパスでなんとかなるはず。IEのセキュリティ設定の「混在したコンテンツを表示する」も動作に影響がある。



最終更新 2011/11/17 23:46:14 - ie_user_css
(2011/05/25 23:56:43 作成)


Page Index
FrontPage
chm.css HTML Help用

アクセス数
11773