aboutsummaryrefslogtreecommitdiffstats
path: root/uaSwitchLite.js
blob: 2b6d796e0f3e11e3baf0354651726ceff3678bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var PLUGIN_INFO =
<VimperatorPlugin>
  <name>UserAgentSwitcherLite</name>
  <description>switch user agent</description>
  <description lang='ja'>user agent 切り替え</description>
  <version>0.1.1</version>
  <author homepage='http://d.hatena.ne.jp/pekepekesamurai/'>pekepeke</author>
  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/uaSwitchLite</updateURL>
  <minVersion>2.0pre</minVersion>
  <maxVersion>2.0pre</maxVersion>
  <detail lang='ja'><![CDATA[
    == Commands ==
      :ua [uaname]:
        User Agent を切り替えます
      :ua:
        User Agent を表示します

    == .vimperatorrrc ==
      >||
        javascript <<EOM
        liberator.globalVariables.useragent_list = [
        {
          description: 'Internet Explorer 7 (Windows Vista)',
          useragent: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
          appname: 'Microsoft Internet Explorer',
          appversion: '4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
          platform: 'Win32',
        }, {
          description: 'Netscape 4.8 (Windows Vista)',
          useragent: 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
          appname: 'Netscape',
          appversion: '4.8 [en] (Windows NT 6.0; U)',
          platform: 'Win32',
        }, {
          description: 'Google',
          useragent: 'Googlebot/2.1 (+http://www.google.com/bot.html)',
        }];
        EOM
      ||<
  ]]></detail>
</VimperatorPlugin>;


liberator.plugins.UserAgentSwitcherLite = (function(){

const USER_AGENT = 'general.useragent.override';
const APP_NAME = 'general.appname.override';
const APP_VERSION = 'general.appversion.override';
const PLATFORM = 'general.platform.override';
const VENDOR = 'general.useragent.vendor';
const VENDOR_SUB = 'general.useragent.vendorSub';
const DEFAULT = 'Default';

var global = liberator.globalVariables;
global.useragent_list = global.useragent_list ? global.useragent_list : [
  {
    description: 'Internet Explorer 7 (Windows Vista)',
    useragent: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
    appname: 'Microsoft Internet Explorer',
    appversion: '4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
    platform: 'Win32',
    vendor: '',
    vendorSub: ''
  }, {
    description: 'Netscape 4.8 (Windows Vista)',
    useragent: 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
    appname: 'Netscape',
    appversion: '4.8 [en] (Windows NT 6.0; U)',
    platform: 'Win32',
    vendor: '',
    vendorSub: ''
  }, {
    description: 'Opera 9.25 (Windows Vista)',
    useragent: 'Opera/9.25 (Windows NT 6.0; U; en)',
    appname: 'Opera',
    appversion: '9.25 (Windows NT 6.0; U; en)',
    platform: 'Win32',
  }
];

var Class = function() function(){ this.initialize.apply(this, arguments); };

var UASwitcherLite = new Class();
UASwitcherLite.prototype = {
  initialize: function(){
    // init
    this.completer = [];
    this.switcher = {
      __noSuchMethod__: function(arg) liberator.echoerr('cannot switch user agent "'+arg+'"'),
      '': function(){
        var ua = options.getPref(USER_AGENT);
        liberator.echo('Current User Agent : ' + (ua ? ua : DEFAULT) );
      }
    };
    var self = this;

    // default values
    this.completer.push([DEFAULT, '']);
    this.switcher[DEFAULT] = function() self.switch();

    // expand setting
    global.useragent_list.forEach( function(item){
      var desc = item.description;
      var userAgent = item.useragent;
      var appName = item.appname;
      var appVersion = item.appversion;
      var platform = item.platform;
      var vendor = item.vendor;
      var vendorSub = item.vendorSub;
      self.completer.push([desc, userAgent]);
      self.switcher[desc] = function() self.switch(appName, appVersion, platform, userAgent, vendor, vendorSub);
    });
    this.registerCommand();
  },
  switch: function(appName, appVersion, platform, userAgent, vendor, vendorSub){
    if (!userAgent && !options.getPref(USER_AGENT)) return;
    var setter = userAgent ? options.setPref : options.resetPref;
    setter(APP_NAME, decodeURIComponent(appName || ''));
    setter(APP_VERSION, decodeURIComponent(appVersion || ''));
    setter(PLATFORM, decodeURIComponent(platform || ''));
    setter(USER_AGENT, decodeURIComponent(userAgent || ''));
    setter(VENDOR, decodeURIComponent(vendor || ''));
    setter(VENDOR_SUB, decodeURIComponent(vendorSub || ''));
  },
  registerCommand: function(){
    var self = this;
    commands.addUserCommand(['ua'], 'Switch User Agent',
      function(arg)
        self.switcher[ (arg.string || arg+'').replace(/\\+/g,'') ](),
      {
        completer: function(context, args){
          var filter = context.filter;
          context.title = ['Description', 'User Agent'];
          if (!filter) {
            context.completions = self.completer;
            return;
          }
          filter = filter.toLowerCase();
          context.completions = self.completer.filter( function(el) el[0].toLowerCase().indexOf(filter) == 0 );
        }
    } );
  }
};
return new UASwitcherLite();
})();
61 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
/* {{{
Copyright (c) 2008-2010, anekos.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. The names of the authors may not be used to endorse or promote products
       derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.


###################################################################################
# http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license
# に参考になる日本語訳がありますが、有効なのは上記英文となります。
###################################################################################

}}} */

// PLUGIN_INFO {{{
let PLUGIN_INFO =
<VimperatorPlugin>
  <name>Stella</name>
  <name lang="ja">すてら</name>
  <description>For Niconico/YouTube/Vimeo, Add control commands and information display(on status line).</description>
  <description lang="ja">ニコニコ動画/YouTube/Vimeo 操作コマンドと情報表示(ステータスライン上に)追加します</description>
  <version>0.22.1</version>
  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
  <license>new BSD License (Please read the source code comments of this plugin)</license>
  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
  <minVersion>2.0</minVersion>
  <maxVersion>2.2pre</maxVersion>
  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/stella.js</updateURL>
  <detail><![CDATA[
    == Commands ==
      :stpl[ay]:
        play or pause
      :stpa[use]:
        pause
      :stvolume <VOLUME>:
        set to the specified volume.
      :stmu[te]:
        turn on/off mute.
      :stre[peat]:
        turn on/off mute.
      :stco[mment]:
        turn on/off comment visible.
      :stse[ek] <TIMECODE>:
        seek to specified position.
        TIMECODE formats
          - :stseek 1:30 # 1分30秒
          label.style.marginRight = (r || 0) + 'px';
          - :stseek 90   # 90
      :stse[ek]! <TIMECODE>:
        seek to the specified position from current position at relatively.
      :stfe[tch]:
        fetch and save the video.
      :stla[rge]:
        enlarge video screen.
      :stfu[llscreen]:
        turn on/off fullscreen.
  ]]></detail>
  <detail lang="ja"><![CDATA[
    == Commands ==
      :stpl[ay]:
        再生/ポーズの解除を行う
      :stpa[use]:
        一時停止する
      :stvolume <VOLUME>:
        指定の音量にする
        0から100の数字で指定する
      :stmu[te]:
        ミュートのOn/Offを切り替える
      :stre[peat]:
        リピートモードのOn/Offを切り替える
      :stco[mment]:
        コメントのOn/Offを切り替える
      :stse[ek] <TIMECODE>:
        指定の秒数までシークする
        TIMECODE は以下の様に指定できる
          - :stseek 1:30 # 1分30秒
          - :stseek 1.5  # 1.590
          - :stseek 90   # 90
      :stse[ek]! <TIMECODE>:
        現在の位置から TIMECODE 分移動する
      :stfe[tch]:
        動画をファイルとして保存する
      :stla[rge]:
        画面を大きくする/戻す
      :stfu[llscreen]:
        フルスクリーン表示のOn/Offを切り替える
    == Controls ==
      マウスのホイール:
        パネル上でホイールの上下することにより音量を上下できます
      時間をクリック:
        再生時間をの表示をクリックすることでシークできます
        左の方をクリックすれば最初の方に右の方をクリックすれば最後の方に跳びます
      アイコンをクリック:
        再生ポーズ
      アイコンをダブルクリック:
        フルスクリーン切り替え
      パネルの cflmr をクリック:
        以下の機能をオンオフします
        (大文字の時がオン)
          C:
            コメント
          F:
            フルスクリーン (Stella によるもの)
          L:
            大画面
          M:
            ミュート(消音)
          R:
            リピート
    == Link ==
      http://d.hatena.ne.jp/nokturnalmortum/20081213/1229168832
  ]]></detail>
</VimperatorPlugin>;
// }}}

/* {{{
TODO
   ・Icons
   ・動的な command の追加削除 (nice rabbit!)
   ・ツールチップみたいな物で、マウスオー馬したときに動画情報を得られるようにしておく。
   ・外から呼ぶべきでない関数(プライベート)をわかりやすくしたい
   ・argCount の指定が適当なのを修正 (動的な userCommand と平行でうまくできそう?)
   ・実際のプレイヤーが表示されるまで待機できようにしたい(未表示に時にフルスクリーン化すると…)
   ・isValid とは別にプレイヤーの準備が出来ているか?などをチェックできる関数があるほうがいいかも
      -> isValid ってなまえはどうなの?
      -> isReady とか
   ・パネルなどの要素にクラス名をつける
   ・上書き保存
   ・Fx の pref と liberator.globalVariables の両方で設定をできるようにする (Setting)

FIXME
    ・this.last.fullscreen = value;

MEMO
   ・prototype での定義順: 単純な値 initialize finalize (get|set)ter メソッド
   ・関数やプロパティは基本的にアルファベット順にならべる。

Refs:
   http://yuichis.homeip.net/nicodai.user.html
   http://coderepos.org/share/browser/lang/javascript/vimperator-plugins/trunk/nicontroller.js
   http://coderepos.org/share/browser/lang/javascript/vimperator-plugins/trunk/youtubeamp.js

Thanks:
   参考にさせてもらった人々。THANKS!!
     janus_wel 氏
       http://d.hatena.ne.jp/janus_wel/
     ゆういち 氏
       http://yuichis.homeip.net/nicodai.user.html
}}} */

(function () {

  /*********************************************************************************
  * Const                                                                        {{{
  *********************************************************************************/

  const ID_PREFIX = 'anekos-stella-';
  const InVimperator = !!(liberator && modules && modules.liberator);
  const DOUBLE_CLICK_INTERVAL = 300;

  // }}}

  /*********************************************************************************
  * Utils                                                                        {{{
  *********************************************************************************/

  const U = {
    bindr: function (_this, f)
      function () f.apply(_this, arguments),

    capitalize: function (s)
      s.replace(/^[a-z]/, String.toUpperCase).replace(/-[a-z]/, function (s) s.slice(1).toUpperCase()),

    currentURL: function ()
      content.document.location.href,

    download: function (url, filepath, ext, title) {
      let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
      let wbp = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist);
      let file;

      if (filepath) {
        file = io.File(io.expandPath(filepath));
      } else {
        file = dm.userDownloadsDirectory;
      }
      if (file.isDirectory() && title)
        file.appendRelativePath(U.fixFilename(title) + ext);
      if (file.exists())
        return liberator.echoerr('The file already exists! -> ' + file.path);
      file = makeFileURI(file);

      let dl = dm.addDownload(0, U.makeURL(url, null, null), file, title, null, null, null, null, wbp);
      wbp.progressListener = dl;
      wbp.persistFlags |= wbp.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
      wbp.saveURI(U.makeURL(url), null, null, null, null, file);

      return true;
    },

    xpathGet: function (xpath, doc, root) {
      if (!doc)
        doc = content.document;
      if (!root)
        root = doc;
      return doc.evaluate(xpath, doc, null, 9, null, 7, null).singleNodeValue;
    },

    xpathGets: function (xpath, doc, root) {
      if (!doc)
        doc = content.document;
      if (!root)
        root = doc;
      let result = [];
      let r = doc.evaluate(xpath, root, null, 7, null);
      for (let i = 0, l = r.snapshotLength; i < l; i++) {
        result.push(r.snapshotItem(i));
      }
      return result;
    },

    fixDoubleClick: function (obj, click, dblClick) {
      let clicked = 0;
      let original = {click: obj[click], dblClick: obj[dblClick]};
      obj[click] = function () {
        let self = this, args = arguments;
        let _clicked = ++clicked;
        setTimeout(function () {
          if (_clicked == clicked--)
            original.click.apply(self, args);
          else
            clicked = 0;
        }, DOUBLE_CLICK_INTERVAL);
      };
      obj[dblClick] = function () {
        clicked = 0;
        original.dblClick.apply(this, arguments);
      };
    },

    fixFilename: function (filename) {
      const badChars = /[\\\/:;*?"<>|]/g;
      return filename.replace(badChars, '_');
    },

    fromTemplate: function (template, args) {
      let index = 0;
      function get (name)
        (args instanceof Array ? args[index++] : args[name]);
      return template.replace(/--([^-]+)--/g, function (_, n) get(n) || '');
    },

    // 上手い具合に秒数に直すよ
    fromTimeCode: function (code) {
      var m;
      if (m = /^(([-+]?)\d+):(\d+)$/(code))
        return parseInt(m[1], 10) * 60 + (m[2] == '-' ? -1 : 1) * parseInt(m[3], 10);
      if (m = /^([-+]?\d+\.\d+)$/(code))
        return Math.round(parseFloat(m[1], 10) * 60);
      return parseInt(code, 10);
    },

    getElementById: function (id)
      content.document.getElementById(id),

    getElementByIdEx: function (id)
      let (p = content.document.getElementById(id))
        (p && (p.wrappedJSObject || p)),

    httpRequest: function (uri, data, onComplete) {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status == 200)
            onComplete && onComplete(xhr);
          else
            U.raise(xhr.statusText);
        }
      };
      xhr.open(data ? 'POST' : 'GET', uri, !!onComplete);
      xhr.send(data || null); // XXX undefined を渡すのはまずいのかな
      return xhr;
    },

    id: function (value)
      value,

    isNum: function (v)
      (typeof v === 'number' && !isNaN(v)),

    lz: function (s, n)
      String(Math.pow(10, n) + s).substring(1),

    makeFile: function (s) {
      var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
      file.initWithPath(s);
      return file;
    },

    makeURL: function (s) {
      let url = Cc["@mozilla.org/network/standard-url;1"].createInstance(Ci.nsIURL);
      url.spec = s;
      return url;
    },

    parseParameter: function (str) {
      let result = {};
      str.split(/&/).forEach(function (it)
                               let ([_, n, v] = it.match(/^([^=]*)=(.*)$/))
                                 (result[n] = unescape(v)));
      return result;
    },

    raise: (InVimperator ? function (error) liberator.echoerr(error)
                         : function (error) {throw new Error(error)}),

    restoreStyle: function (target, doDelete) {
      let style = target.style;
      if (!style.__stella_backup)
        return;
      let backup = style.__stella_backup;
      for (let name in Iterator(backup))
        style[name] = backup[name];
      if (doDelete)
        delete style.__stella_backup;
    },

    s2b: function (s, d) (!/^(\d+|false)$/i.test(s)|parseInt(s)|!!d*2)&1<<!s,

    storeStyle: function (target, values, overwrite) {
      let [style, cstyle] = [target.style, content.getComputedStyle(target, '')];
      let backup = {};
      for (let [name, value] in Iterator(values)) {
        backup[name] = cstyle[name];
        style[name] = value;
      }
      if (overwrite || !style.__stella_backup)
        style.__stella_backup = backup;
    },

    toTimeCode: function (v)
      (U.isNum(v) ? (parseInt((v / 60)) + ':' + U.lz(v % 60, 2))
                : '??:??')
  };


  // }}}

  /*********************************************************************************
  * Setting                                                                      {{{
  *********************************************************************************/

  function Setting () {
    this.niconico = {
      autoFullscreenDelay: 4000
    };
  }

  // }}}

  /*********************************************************************************
  * Player                                                                       {{{
  *********************************************************************************/

  function Player (stella) {
    let self = this;

    this.initialize.apply(this, arguments);

    this.stella = stella;

    this.last = {
      fullscreen: false
    };

    function setf (name, value)
      ((self.functions[name] === undefined) && (self.functions[name] = value || ''));

    let (seek = this.has('currentTime', 'rw', 'totalTime', 'r') && 'x') {
      setf('seek', seek);
      setf('seekRelative', seek);
    }
    setf('playOrPause', this.has('play', 'x', 'pause', 'x') && 'x');
    setf('turnUpDownVolume', this.has('volume', 'rw') && 'x');
    setf('maxVolume', this.has('volume', 'rw') && 'r');
    setf('fetch', this.has('fileURL', 'r') && 'x');
    setf('relations', [name for each (name in Player.RELATIONS) if (this.has(name, 'r'))].length && 'r');
    if (!this.functions.large)
      this.functions.large = this.functions.fullscreen;
  }

  Player.ST_ENDED   = 'ended';
  Player.ST_OTHER   = 'other';
  Player.ST_PAUSED  = 'paused';
  Player.ST_PLAYING = 'playing';

  Player.URL_ID     = 'id';
  Player.URL_SEARCH = 'search';
  Player.URL_TAG    = 'tag';
  Player.URL_URL    = 'url';

  Player.RELATIONS = {
    URL_TAG: 'relatedTags',
    URL_ID: 'relatedIDs'
  };

  // rwxt で機能の有無を表す
  // r = read
  // w = write
  // x = function
  // t = toggle
  Player.prototype = {
    functions: {
      comment: '',
      currentTime: '',
      fileExtension: 'r',
      fileURL: '',
      fullscreen: '',
      large: '',
      makeURL: '',
      muted: '',
      pause: '',
      play: '',
      playEx: '',
      relatedIDs: '',
      relatedTags: '',
      repeating: '',
      say: '',
      tags: '',
      title: '',
      totalTime: '',
      volume: '',
      // auto setting => fetch maxVolume playOrPause relations seek seekRelative turnUpDownVolume
    },

    icon: null,

    initialize: function () void null,

    finalize: function () {
      // 念のためフルスクリーンは解除しておく
      if (this.has('fullscreen', 'rwt') && this.isValid && this.fullscreen)
        this.fullscreen = false;
    },

    is: function (state) (this.state == state),

    has: function (name, ms)
      (arguments.length < 2)
      ||
      let (f = this.functions[name])
        (f && !Array.some(ms, function (m) f.indexOf(m) < 0))
        &&
        arguments.callee.apply(this, Array.splice(arguments, 2)),

    get currentTime () undefined,
    set currentTime (value) value,

    get fileExtension () '',

    get fullscreen () undefined,
    set fullscreen (value) value,

    get fileURL () undefined,

    get large () this.fullscreen,
    set large (value) (this.fullscreen = value),

    get maxVolume () 100,

    get muted () undefined,
    set muted (value) value,

    get ready () undefined,

    get relatedIDs () undefined,

    get relations () {
      if (!this.has('relations', 'r'))
        return [];
      let result = [];
      for (let [type, name] in Iterator(Player.RELATIONS)) {
        if (this.has(name, 'r'))
          result = result.concat(this[name]);
      }
      return result;
    },

    get repeating () undefined,
    set repeating (value) value,

    get state () undefined,

    get statusText () this.timeCodes,

    get storage ()
      (content.document.__stella_player_storage || (content.document.__stella_player_storage = {})),

    get timeCodes () (U.toTimeCode(this.currentTime) + '/' + U.toTimeCode(this.totalTime)),

    get title () undefined,

    get isValid () /^http:\/\/(tw|es|de|www)\.nicovideo\.jp\/watch\//.test(buffer.URL),

    get volume () undefined,
    set volume (value) value,

    fetch: function (filepath)
      U.download(this.fileURL, filepath, this.fileExtension, this.title),

    makeURL: function () undefined,

    pause: function () undefined,

    play: function () undefined,

    playEx: function () {
      if (this.is(Player.ST_ENDED))
        this.currentTime = 0;
      this.play();
    },

    playOrPause: function () {
      if (this.is(Player.ST_PLAYING)) {
        this.pause();
      } else {
        this.playEx();
      }
    },

    seek: function (v) {
      v = U.fromTimeCode(v);
      if (v < 0)
        v = this.totalTime + v;
      return this.currentTime = Math.min(Math.max(v, 0), this.totalTime);
    },

    seekRelative: function (v)
      this.currentTime = Math.min(Math.max(this.currentTime + U.fromTimeCode(v), 0), this.totalTime),

    toggle: function (name) {
      if (!this.has(name, 'rwt'))
        return;
      let v = this[name];
      this[name] = !v;
      return !v;
    },

    turnUpDownVolume: function (v)
      this.volume = Math.min(Math.max(this.volume + parseInt(v), 0), this.maxVolume)
  };

  // }}}

  /*********************************************************************************
  * Relation                                                                     {{{
  *********************************************************************************/

  function Relation () {
  }

  Relation.prototype = {
    get command () undefined,
    get description () undefined,
    get completionItem () ([this.command, this.description]),
  };


  // }}}

  /*********************************************************************************
  * Relation - Sub                                                               {{{
  *********************************************************************************/

  function RelatedTag (tag) {
    this.tag = tag;
    Relation.apply(this, arguments);
  }

  RelatedTag.prototype = {
    __proto__: Relation.prototype,
    get command () (':' + this.tag),
    get description () (this.tag)
  };



  function RelatedID (id, title) {
    this.id = id;
    this.title = title;
    Relation.apply(this, arguments);
  }

  RelatedID.prototype = {
    __proto__: Relation.prototype,
    get command () ('#' + this.id),
    get description () this.title
  };



  function RelatedURL (url, title) {
    this.url = url;
    this.title = title;
    Relation.apply(this, arguments);
  }

  RelatedURL.prototype = {
    __proto__: Relation.prototype,
    get command () this.url,
    get description () this.title
  };

  // }}}

  /*********************************************************************************
  * YouTubePlayer                                                                {{{
  *********************************************************************************/

  function YouTubePlayer () {
    Player.apply(this, arguments);
  }

  YouTubePlayer.getIDfromURL = function (url) let ([_, r] = url.match(/[?;&]v=([-\w]+)/)) r;

  YouTubePlayer.OUTER_NODES = [
    'old-masthead',
    'watch-vid-title',
    'watch-other-vids',
    'old-footer',
    'copyright',
    'watch-main-area',
    'watch-comments-stats',
    'watch-video-response',
    'chrome-promo',
    'watch-video-quality-setting',
  ];

  YouTubePlayer.prototype = {
    __proto__: Player.prototype,

    functions: {
      currentTime: 'rw',
      fileURL: 'r',
      fullscreen: 'rwt',
      makeURL: 'x',
      muted: 'rwt',
      pause: 'x',
      play: 'x',
      playEx: 'x',
      playOrPause: 'x',
      relatedIDs: 'r',
      repeating: '',
      title: 'r',
      totalTime: 'r',
      volume: 'rw'
    },

    icon: 'http://www.youtube.com/favicon.ico',

    get currentTime () parseInt(this.player.getCurrentTime()),
    set currentTime (value) (this.player.seekTo(U.fromTimeCode(value)), this.currentTime),

    get fileExtension () '.mp4',

    get fileURL ()
      let (as = content.document.defaultView.wrappedJSObject.swfArgs)
        ('http://www.youtube.com/get_video?fmt=22&video_id=' + as.video_id + '&t=' + as.t),

    get fullscreen () this.storage.fullscreen,
    // FIXME - うまく元に戻らないことがある?
    set fullscreen (value) {
      function changeOuterNodes (hide) {
        return;
        const st = {display: 'none'};
        let f = hide ? function (node) U.storeStyle(node, st)
                     : function (node) U.restoreStyle(node);
        YouTubePlayer.OUTER_NODES.forEach(
          function (id) {
            let (node = U.getElementById(id)) {
              node && f(node);
            }
          }
        );
      }

      this.last.fullscreen = value;
      this.storage.fullscreen = value;

      // changeOuterNodes(value);

      let p = this.player;
      let r = p.getBoundingClientRect();
      if (this.fullscreen) {
        if (this.storage.r === undefined)
          this.storage.r = options['guioptions'].indexOf('r') >= 0;
        U.storeStyle(p, {
          marginLeft: -r.left + 'px',
          marginTop: -r.top + 'px',
          width: content.innerWidth + 'px',
          height: content.innerHeight + 'px',
        });
        p.setSize(content.innerWidth, content.innerHeight);
      } else {
        p.setSize(640, 385);
        U.restoreStyle(p);
      }
    },

    get muted () this.player.isMuted(),
    set muted (value) ((value ? this.player.mute() : this.player.unMute()), value),

    get player ()
      U.getElementByIdEx('movie_player'),

    get ready () !!this.player,

    get relatedIDs () {
      let result = [];
      let doc = content.document;
      let r = doc.evaluate("//div[@class='video-mini-title']/a", doc, null, 7, null);
      for (let i = 0, l = r.snapshotLength; i < l; i++) {
        let e = r.snapshotItem(i);
        result.push(new RelatedID(YouTubePlayer.getIDfromURL(e.href), e.textContent));
      }
      return result;
    },

    get state () {
      switch (this.player.getPlayerState()) {
        case 0:
          return Player.ST_ENDED;
        case 1:
          return Player.ST_PLAYING;
        case 2:
          return Player.ST_PAUSED;
        case 3:
          // buffering
        case 5:
          //video cued
        case -1:
          //unstarted
        default:
          return Player.ST_OTHER;
      }
    },

    get title ()
      content.document.title.replace(/^YouTube - /, ''),

    get totalTime () parseInt(this.player.getDuration()),

    get isValid () buffer.URL.match(/^http:\/\/(?:[^.]+\.)?youtube\.com\/watch/),

    get volume () parseInt(this.player.getVolume()),
    set volume (value) (this.player.setVolume(value), this.volume),

    makeURL: function (value, type) {
      switch (type) {
        case Player.URL_ID:
          return 'http://www.youtube.com/watch?v=' + value + '&fmt=22'; //XXX さりげなく高画質に!
        case Player.URL_SEARCH:
          return 'http://www.youtube.com/results?search_query=' + encodeURIComponent(value);
      }
      return value;
    },

    play: function () this.player.playVideo(),

    pause: function () this.player.pauseVideo()
  };

  // }}}

  /*********************************************************************************
  * NicoPlayer                                                                   {{{
  *********************************************************************************/

  function NicoPlayer () {
    Player.apply(this, arguments);
  }

  // name, Normal, Fullscreen
  // 任意の順で設定できるように配列で持つ
  NicoPlayer.Variables = [
    ['videowindow._xscale',                     100,   null],
    ['videowindow._yscale',                     100,   null],
    ['videowindow._x',                            6,      0],
    ['videowindow._y',                           65,      0],
    ['controller._x',                             6,  -1000],
    ['inputArea._x',                              4,  -1000],
    ['controller._visible',                       1,      1],
    ['inputArea._visible',                        1,      1],
    ['waku._visible',                             1,      0],
    ['tabmenu._visible',                          1,      0],
    ['videowindow.video_mc.video.smoothing',   null,      1],
    ['videowindow.video_mc.video.deblocking',  null,      5]
  ];

  NicoPlayer.SIZE_LARGE  = 'fit';
  NicoPlayer.SIZE_NORMAL = 'normal';

  NicoPlayer.prototype = {
    __proto__: Player.prototype,

    functions: {
      comment: 'rwt',
      currentTime: 'rw',
      fetch: 'x',
      fileURL: '',
      fullscreen: 'rwt',
      id: 'r',
      large: 'rwt',
      makeURL: 'x',
      muted: 'rwt',
      pause: 'x',
      play: 'x',
      playEx: 'x',
      playOrPause: 'x',
      relatedIDs: 'r',
      relatedTags: 'r',
      repeating: 'rwt',
      say: 'x',
      tags: 'r',
      title: 'r',
      totalTime: 'r',
      volume: 'rw'
    },

    icon: 'http://www.nicovideo.jp/favicon.ico',

    initialize: function () {
      this.__info_cache = {};
    },

    get baseURL () 'http://www.nicovideo.jp/',

    get cachedInfo () {
      let url = U.currentURL();
      if (this.__info_cache.url != url)
        this.__info_cache = {url: url};
      return this.__info_cache;
    },

    get comment () this.player.ext_isCommentVisible(),
    set comment (value) (this.player.ext_setCommentVisible(value), value),

    get currentTime () {
      try {
      return parseInt(this.player.ext_getPlayheadTime())
      } catch (e) {
        liberator.log(e)
        liberator.log(e.stack)
      }
    },
    set currentTime (value) (this.player.ext_setPlayheadTime(U.fromTimeCode(value)), this.currentTime),

    get fileExtension () '.flv',

    get fullscreen () this.large,
    set fullscreen (value) (this.large = value),

    get id ()
      let (m = U.currentURL().match(/\/watch\/([a-z]{2}\d+)/))
        (m && m[1]),

    get muted () this.player.ext_isMute(),
    set muted (value) (this.player.ext_setMute(value), value),

    get player () U.getElementByIdEx('flvplayer'),

    get playerContainer () U.getElementByIdEx('flvplayer_container'),

    get ready () !!(this.player && this.player.ext_getVideoSize),

    get relatedIDs () {
      if (this.__rid_last_url == U.currentURL())
        return this.__rid_cache || [];

      let videos = [];
      let failed = false;

      // API で取得
      try {
        let uri = 'http://www.nicovideo.jp/api/getrelation?sort=p&order=d&video=' + this.id;
        let xhr = new XMLHttpRequest();
        xhr.open('GET', uri, false);
        xhr.send(null);
        let xml = xhr.responseXML;
        let v, vs = xml.evaluate('//video', xml, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        while (v = vs.iterateNext()) {
          let [cs, video] = [v.childNodes, {}];
          for each (let c in cs)
            if (c.nodeName != '#text')
              video[c.nodeName] = c.textContent;
          videos.push(new RelatedID(video.url.replace(/^.+?\/watch\//, ''), video.title));
        }
      } catch (e) {
        liberator.log('stella: ' + e)
        failed = true;
      }

      // コメント欄からそれっぽいのを取得する
      // コメント欄のリンクの前のテキストをタイトルと見なす
      // textContent を使うと改行が理解できなくなるので、innerHTML で頑張ったけれど頑張りたくない
      try {
        let xpath = 'id("des_2")/table[2]/tbody/tr/td[2]';
        let comment = U.xpathGet(xpath).innerHTML;
        let links = U.xpathGets(xpath + '/p/a')
                     .filter(function (it) /watch\//.test(it.href))
                     .map(function(v) v.textContent);
        links.forEach(function (link) {
          let re = RegExp('(?:^|[\u3000\\s\\>])([^\u3000\\s\\>]+)\\s*<a href="http:\\/\\/www\\.nicovideo\\.\\w+\\/watch\\/' + link + '" class="(watch|video)">');
          let r = re.exec(comment);
          if (r)
            videos.push(new RelatedID(link, r[1].slice(-20)));
        });
      } catch (e) {
        liberator.log('stella: ' + e)
        //failed = true;
      }

      if (!failed) {
        this.__rid_last_url = U.currentURL();
        this.__rid_cache = videos;
      }

      return videos;
    },

    get relatedTags() {
      let nodes = content.document.getElementsByClassName('nicopedia');
      return [new RelatedTag(it.textContent) for each (it in nodes) if (it.rel == 'tag')];
    },

    get repeating () this.player.ext_isRepeat(),
    set repeating (value) (this.player.ext_setRepeat(value), value),

    get large () this.player.ext_getVideoSize() === NicoPlayer.SIZE_LARGE,
    set large (value) {
        if (value && !this.large) {
          let win = Buffer.findScrollableWindow();
          this.storage.scrollPositionBeforeLarge = {x: win.scrollX, y: win.scrollY};
        }

        this.player.ext_setVideoSize(value ? NicoPlayer.SIZE_LARGE : NicoPlayer.SIZE_NORMAL);

        let pos = this.storage.scrollPositionBeforeLarge;
        if (!value && typeof pos != "undefined")
            setTimeout(function () buffer.scrollTo(pos.x, pos.y), 0);

        return this.large;
    },

    get state () {
      switch (this.player.ext_getStatus()) {
        case 'end':
          return Player.ST_ENDED;
        case 'playing':
          return Player.ST_PLAYING;
        case 'paused':
          return Player.ST_PAUSED;
        case 'buffering':
        default:
          return Player.ST_OTHER;
      }
    },

    get title () content.document.title.replace(/\s*\u2010\s*\u30CB\u30B3\u30CB\u30B3\u52D5\u753B(.+)$/, ''),

    get totalTime () parseInt(this.player.ext_getTotalTime()),

    get volume () parseInt(this.player.ext_getVolume()),
    set volume (value) (this.player.ext_setVolume(value), this.volume),

    fetch: function (filepath) {
      let onComplete = U.bindr(this, function (xhr) {
          let res = xhr.responseText;
          let info = {};
          res.split(/&/).forEach(function (it) let ([n, v] = it.split(/=/)) (info[n] = v));
          U.download(decodeURIComponent(info.url), filepath, this.fileExtension, this.title);
      });
      U.httpRequest('http://www.nicovideo.jp/api/getflv?v=' + this.id, null, onComplete);
    },

    makeURL: function (value, type) {
      switch (type) {
        case Player.URL_ID:
          return 'http://www.nicovideo.jp/watch/' + value;
        case Player.URL_TAG:
          return 'http://www.nicovideo.jp/tag/' + encodeURIComponent(value);
        case Player.URL_SEARCH:
          return 'http://www.nicovideo.jp/search/' + encodeURIComponent(value);
      }
      return value;
    },

    pause: function () this.player.ext_play(false),

    play: function () this.player.ext_play(true),

    playOrPause: function () {
      if (this.is(Player.ST_PLAYING)) {
        this.pause();
      } else {
        let base = this.currentTime;
        setTimeout(U.bindr(this, function () (base === this.currentTime ? this.playEx() : this.pause())), 100);
      }
    },

    say: function (message) {
      liberator.log('stsay');
      this.sendComment(message);
    },

    // みかんせいじん
    // test -> http://www.nicovideo.jp/watch/sm2586636
    // 自分のコメントが見れないので、うれしくないかも。
    sendComment: function (message, command, vpos) {
      let self = this;

      // コメント連打を防止
      {
        let now = new Date();
        let last = this.__last_comment_time;
        if (last && (now.getTime() - last.getTime()) < 5000)
          return U.raise('Shurrup!!');
        this.__last_comment_time = now;
      }

      function getThumbInfo () {
        liberator.log('getThumbInfo');
        if (self.cachedInfo.block_no !== undefined)
          return;
        let xhr = U.httpRequest(self.baseURL + 'api/getthumbinfo/' + self.id);
        let xml = xhr.responseXML;
        let cn = xml.getElementsByTagName('comment_num')[0];
        self.cachedInfo.block_no = cn.textContent.replace(/..$/, '');
      }

      function getFLV () {
        liberator.log('getFLV');
        if (self.cachedInfo.flvInfo !== undefined)
          return;
        let xhr = U.httpRequest(self.baseURL + 'api/getflv?v=' + self.id);
        let res = xhr.responseText;
        self.cachedInfo.flvInfo = U.parseParameter(res);
      }

      function getPostkey () {
        liberator.log('getPostkey');
        let info = self.cachedInfo;
        if (info.postkey !== undefined)
          return;
        let url = U.fromTemplate(
                    '--base--api/getpostkey?thread=--thread_id--&block_no=--block_no--',
                    {
                      base: self.baseURL,
                      thread_id: info.flvInfo.thread_id,
                      block_no: info.block_no
                    }
                  );
        liberator.log(url);
        let xhr = U.httpRequest(url);
        let res = xhr.responseText;
        info.postkey = res.replace(/^.*=/, '');
      }

      function getComments () {
        liberator.log('getComments');
        let info = self.cachedInfo;
        if (info.ticket !== undefined)
          return;
        let tmpl = '<thread res_from="-1" version="20061206" thread="--thread_id--"/>';
        let xhr = U.httpRequest(info.flvInfo.ms, U.fromTemplate(tmpl, info.flvInfo));
        let xml = xhr.responseXML;
        let r = xml.evaluate('//packet/thread', xml, null, 9, null, 7, null).singleNodeValue;
        info.ticket = r.getAttribute('ticket');
      }

      function sendChat () {
        liberator.log('sendChat');
        let info = self.cachedInfo;
        let tmpl = '<chat premium="--is_premium--" postkey="--postkey--" user_id="--user_id--" ticket="--ticket--" mail="--mail--" vpos="--vpos--" thread="--thread_id--">--body--</chat>';
        let args = {
          __proto__: info.flvInfo,
          ticket: info.ticket,
          postkey: info.postkey,
          // 0 秒コメントはうざいらしいので勝手に自重する
          vpos: Math.max(100, parseInt(vpos || (self.player.ext_getPlayheadTime() * 100), 10)),
          body: message
        };
        liberator.log(args);
        let data = U.fromTemplate(tmpl, args);
        let xhr = U.httpRequest(info.flvInfo.ms, data);
        liberator.log(xhr.responseText);
      }

      liberator.log('sendcommnet');
      getThumbInfo();
      getFLV();
      getPostkey();
      getComments();
      sendChat();
    }
  };

  // }}}

  /*********************************************************************************
  * VimeoPlayer                                                                  {{{
  *********************************************************************************/

  function VimeoPlayer () {
    Player.apply(this, arguments);
  }

  VimeoPlayer.getIDfromURL = function (url) let ([_, r] = url.match(/[?;&]v=([-\w]+)/)) r;

  VimeoPlayer.prototype = {
    __proto__: Player.prototype,

    functions: {
      currentTime: 'w',
      fileURL: '',
      fullscreen: '',
      makeURL: 'x',
      muted: 'w',
      pause: 'x',
      play: 'x',
      playEx: 'x',
      playOrPause: 'x',
      relatedIDs: '',
      repeating: '',
      title: 'r',
      totalTime: '',
      volume: ''
    },

    __initializePlayer: function (player) {
      if (!player || player.__stella_initialized)
        return player;

      player.__stella_mute = false;
      player.__stella_volume = 100;
      player.__stella_initialized = true;

      return player;
    },

    icon: 'http://www.vimeo.com/favicon.ico',

    set currentTime (value) (this.player.api_seekTo(U.fromTimeCode(value)), value),

    get muted () this.__mute,
    set muted (value) (this.volume = value ? 0 : 100),

    get player ()
      this.__initializePlayer(U.xpathGet('//embed[contains(@id,"vimeo_clip_")]').wrappedJSObject),

    get ready () !!this.player,

    get state () {
      if (this.player.api_isPlaying())
        return Player.ST_PLAYING
      if (this.player.api_isPaused())
        return Player.ST_PAUSED;
      return Player.ST_OTHER;
    },

    get title ()
      U.xpathGet('//div[@class="title"]').textContent,

    get isValid () buffer.URL.match(/^http:\/\/(www\.)?vimeo\.com\/(channels\/(hd)?#)?\d+$/),

    // XXX setVolume は実際には存在しない?
    get volume () parseInt(this.player.__stella_volume),
    set volume (value) (this.api_setVolume(value), this.player.__stella_volume = value),

    makeURL: function (value, type) {
      switch (type) {
        case Player.URL_ID:
          return 'http://www.vimeo.com/' + value;
        case Player.URL_SEARCH:
          return 'http://www.vimeo.com/videos/search:' + encodeURIComponent(value);
      }
      return value;
    },

    play: function () this.player.api_play(),

    pause: function () this.player.api_pause()
  };

  // }}}

  /*********************************************************************************
  * ContextMenu                                                                  {{{
  *********************************************************************************/

  const ContextMenuVolume = [];
  for (let i = 0; i <= 100; i += 10)
    ContextMenuVolume.push({name: 'setVolume', label: i + '%', attributes: {volume: i}});

  const ContextMenuTree = [
    'play',
    'pause',
    'comment',
    'repeat',
    'fullscreen',
    'fetch',
    {
      name: 'volume-root',
      label: 'Volume',
      id: ID_PREFIX + 'volume-menupopup',
      sub: ContextMenuVolume
    },
    {
      name: 'relations-root',
      label: 'Relations',
      id: ID_PREFIX + 'relations-menupopup',
      sub: []
    },
    'cancel',
  ];

  function buildContextMenu (setting) {
    function append (parent, menu) {
      if (typeof menu == 'string')
        menu = {name: menu};
      if (menu instanceof Array)
        return menu.forEach(function (it) append(parent, it));
      if (!menu.label)
        menu.label = U.capitalize(menu.name);
      let (elem) {
        if (menu.sub) {
          let _menu = document.createElement('menu');
          let _menupopup = elem = document.createElement('menupopup');
          _menu.setAttribute('label', menu.label);
          _menu.appendChild(_menupopup);
          parent.appendChild(_menu);
          append(_menupopup, menu.sub);
        } else {
          elem = document.createElement('menuitem');
          elem.setAttribute('label', menu.label);
          parent.appendChild(elem);
        }
        menu.id && elem.setAttribute('id', menu.id);
        for (let [name, value] in Iterator(menu.attributes || {}))
          elem.setAttribute(name, value);
        setting.onAppend.call(setting, elem, menu);
      }
    }

    let root = document.createElement('menupopup');
    root.id = setting.id;

    append(root, setting.tree);

    setting.set.setAttribute('context', root.id);
    setting.parent.appendChild(root);

    return root;
  }

  // }}}

  /*********************************************************************************
  * Event                                                                        {{{
  *********************************************************************************/

  function WebProgressListener (listeners) {
    let self = this;
    for (let [name, listener] in Iterator(listeners))
      this[name] = listener;
    getBrowser().addProgressListener(this);
    // これは必要?
    window.addEventListener('unload', U.bindr(this.uninstall), false);
  }

  WebProgressListener.prototype = {
    onStatusChange: function (webProgress, request, stateFlags, staus) undefined,
    onProgressChange: function (webProgress, request, curSelfProgress,
                                maxSelfProgress, curTotalProgress, maxTotalProgress) undefined,
    onLocationChange: function (webProgress, request, location) undefined,
    onStateChange: function (webProgress, request, status, message) undefined,
    onSecurityChange: function (webProgress, request, state) undefined,
    uninstall: function () getBrowser().removeProgressListener(this)
  };

  // }}}

  /*********************************************************************************
  * Stella                                                                       {{{
  *********************************************************************************/

  function Stella (setting) {
    this.initialize.apply(this, arguments);
    this.setting = setting;
  }

  Stella.MAIN_PANEL_ID  = ID_PREFIX + 'main-panel',
  Stella.MAIN_MENU_ID   = ID_PREFIX + 'main-menu',
  Stella.VOLUME_MENU_ID = ID_PREFIX + 'volume-menu',

  Stella.prototype = {
    // new 時に呼ばれる
    initialize: function () {
      let self = this;

      this.players = {
        niconico: new NicoPlayer(this.stella),
        youtube: new YouTubePlayer(this.stella),
        vimeo: new VimeoPlayer(this.stella)
      };

      this.createStatusPanel();
      this.onLocationChange();
      this.hidden = true;

      this.__onResize = window.addEventListener('resize', U.bindr(this, this.onResize), false);
      this.progressListener = new WebProgressListener({onLocationChange: U.bindr(this, this.onLocationChange)});
    },

    // もちろん、勝手に呼ばれたりはしない。
    finalize: function () {
      this.removeStatusPanel();
      this.disable();
      this.progressListener.uninstall();
      for each (let player in this.players)
        player.finalize();
      window.removeEventListener('resize', this.__onResize, false);
    },

    get hidden () (this.panel.hidden),
    set hidden (v) (this.panel.hidden = v),

    get isValid () (this.where),

    get player () (this.where && this.players[this.where]),

    get statusBar () document.getElementById('status-bar'),

    get statusBarVisible () !this.statusBar.getAttribute('moz-collapsed', false),
    set statusBarVisible (value) (this.statusBar.setAttribute('moz-collapsed', !value), value),

    get storage ()
      (content.document.__stella_storage || (content.document.__stella_storage = {})),

    get where () {
      for (let [name, player] in Iterator(this.players))
        if (player.isValid)
          return name;
    },

    addUserCommands: function () {
      let self = this;

      function add (cmdName, funcS, funcB) {
        commands.addUserCommand(
          ['st' + cmdName],
          cmdName.replace(/[\[\]]+/g, '') + ' - Stella',
          (funcS instanceof Function)
            ? funcS
            : function (arg) {
                if (!self.isValid)
                  U.raise('Stella: Current page is not supported');
                let p = self.player;
                let func = arg.bang ? funcB : funcS;
                if (p.has(func, 'rwt'))
                  p.toggle(func);
                else if (p.has(func, 'rw'))
                  p[func] = arg[0];
                else if (p.has(func, 'x'))
                  p[func].apply(p, arg);
                else
                  U.raise('Stella: The function is not supported in this page.');
                self.update();
              },
          {argCount: '*', bang: !!funcB},
          true
        );
      }

      add('pl[ay]', 'playOrPause', 'play');
      add('pa[use]', 'pause');
      add('mu[te]', 'muted');
      add('re[peat]', 'repeating');
      add('co[mment]', 'comment');
      add('vo[lume]', 'volume', 'turnUpDownVolume');
      add('se[ek]', 'seek', 'seekRelative');
      add('fe[tch]', 'fetch');
      add('la[rge]', 'large');
      add('fu[llscreen]', 'fullscreen');
      if (U.s2b(liberator.globalVariables.stella_use_nico_comment, false))
        add('sa[y]', 'say');

      commands.addUserCommand(
        ['strel[ations]'],
        'relations - Stella',
        function (args) {
          let arg = args.string;
          let url = self.player.has('makeURL', 'x') ? makeRelationURL(self.player, arg) : arg;
          liberator.open(url, args.bang ? liberator.NEW_TAB : liberator.CURRENT_TAB);
        },
        {
          argCount: '*',
          bang: true,
          completer: function (context, args) {
            if (!self.isValid)
              U.raise('Stella: Current page is not supported');
            if (!self.player.has('relations', 'r'))
              return;
            context.title = ['Tag/ID', 'Description'];
            context.completions = self.player.relations.map(function (rel) rel.completionItem);
          },
        },
        true
      );
    },

    createStatusPanel: function () {
      let self = this;

      function setEvents (name, elem) {
        ['click', 'popupshowing'].forEach(function (eventName) {
          let onEvent = self['on' + U.capitalize(name) + U.capitalize(eventName)];
          onEvent && elem.addEventListener(eventName, function (event) {
            if (eventName != 'click' || event.button == 0) {
              onEvent.apply(self, arguments);
              self.update();
            }
          }, false);
        });
      }

      function createLabel (store, name, l, r) {
          let label = store[name] = document.createElement('label');
          label.setAttribute('value', '-');
          label.style.marginLeft = (l || 0) + 'px';
          label.style.marginRight = (r || 0) + 'px';
          label.__defineGetter__('text', function () this.getAttribute('value'));
          label.__defineSetter__('text', function (v) this.setAttribute('value', v));
          setEvents(name, label);
      }

      let panel = this.panel = document.createElement('statusbarpanel');
      panel.setAttribute('id', Stella.MAIN_PANEL_ID);

      let hbox = document.createElement('hbox');
      hbox.setAttribute('align', 'center');

      let icon = this.icon = document.createElement('image');
      icon.setAttribute('class', 'statusbarpanel-iconic');
      icon.style.marginRight = '4px';
      setEvents('icon', icon);
      icon.addEventListener('dblclick', U.bindr(this, this.onIconDblClick), false);

      let labels = this.labels = {};
      let toggles = this.toggles = {};
      createLabel(labels, 'main', 2, 2);
      createLabel(labels, 'volume', 0, 2);
      for each (let player in this.players) {
        for (let func in player.functions) {
          if (player.has(func, 't'))
            (func in labels) || createLabel(toggles, func);
        }
      }

      panel.appendChild(hbox);
      hbox.appendChild(icon);
      [hbox.appendChild(label) for each (label in labels)];
      [hbox.appendChild(toggle) for each (toggle in toggles)];

      let menu = this.mainMenu = buildContextMenu({
        id: Stella.MAIN_MENU_ID,
        parent: panel,
        set: hbox,
        tree: ContextMenuTree,
        onAppend: function (elem, menu) setEvents(U.capitalize(menu.name), elem)
      });

      let stbar = document.getElementById('status-bar');
      stbar.insertBefore(panel, document.getElementById('liberator-statusline').nextSibling);

      let relmenu = document.getElementById('anekos-stella-relations-menupopup');

      panel.addEventListener('DOMMouseScroll', U.bindr(this, this.onMouseScroll), true);
    },

    disable: function () {
      this.hidden = true;
      if (this.__updateTimer) {
        clearInterval(this.__updateTimer);
        delete this.__updateTimer;
      }
      if (this.__autoFullscreenTimer) {
        clearInterval(this.__autoFullscreenTimer);
      }
    },

    enable: function () {
      this.hidden = false;
      this.icon.setAttribute('src', this.player.icon);
      for (let name in this.toggles) {
        this.toggles[name].hidden = !this.player.has(name, 't');
      }
      if (!this.__updateTimer) {
        this.__updateTimer = setInterval(U.bindr(this, this.update), 500);
      }
    },

    removeStatusPanel: function () {
      let e = this.panel || document.getElementById(this.panelId);
      if (e && e.parentNode)
        e.parentNode.removeChild(e);
    },

    update: function () {
      if (!(this.isValid && this.player.ready))
        return;
      this.labels.main.text =
        let (v = this.player.statusText)
          (this.__currentTimeTo == undefined) ? v
                                              : v.replace(/^\d*\:\d*/,
                                                          U.toTimeCode(this.__currentTimeTo));
      this.labels.volume.text = this.player.volume;
      for (let name in this.toggles) {
        this.toggles[name].text = (this.player[name] ? String.toUpperCase : U.id)(name[0]);
      }
    },

    onCommentClick: function () (this.player.toggle('comment')),

    onFetchClick: function () this.player.fetch(),

    // フルスクリーン時にステータスバーを隠さないようにする
    onFullScreen: function () {
      if (window.fullScreen) {
        this.__statusBarVisible = this.statusBarVisible;
        this.statusBarVisible = true;
      } else {
        if (this.__statusBarVisible !== undefined)
          this.statusBarVisible = this.__statusBarVisible;
      }
    },

    onFullscreenClick: function () this.player.toggle('fullscreen'),

    onIconClick: function () this.player.playOrPause(),

    onIconDblClick: function () this.player.toggle('fullscreen'),

    onLargeClick: function () {
      // XXX fullscreen と large の実装が同じ場合に問題になるので、toggle は使わない
      let old = this.player.large;
      if (this.player.fullscreen)
        this.player.fullscreen = false;
      this.player.large = !old;
    },

    onLocationChange: function () {
      if (this.__valid !== this.isValid) {
        (this.__valid = this.isValid) ? this.enable() : this.disable();
      }
      if (this.isValid) {
        clearInterval(this.__onReadyTimer);
        this.__onReadyTimer = setInterval(
          U.bindr(this, function () {
            if (this.player && this.player.ready) {
              clearInterval(this.__onReadyTimer);
              delete this.__onReadyTimer;
              this.onReady();
            }
          }),
          200
        );
      }
    },

    onMainClick: function (event) {
      if (event.button)
        return;
      if (!(this.player && this.player.has('currentTime', 'rw', 'totalTime', 'r')))
        return;

      let rect = event.target.getBoundingClientRect();
      let x = event.screenX;
      let per = (x - rect.left) / (rect.right - rect.left);
      this.player.currentTime = parseInt(this.player.totalTime * per);
    },

    onMouseScroll: (function () {
      let timerHandle;
      return function (event) {
        if (!(this.isValid && this.player.ready && event.detail))
          return;
        if (event.target == this.labels.main) {
          if (this.__currentTimeTo == undefined)
            this.__currentTimeTo = this.player.currentTime;
          this.__currentTimeTo += (event.detail > 0) ? -5 : 5;
          this.__currentTimeTo = Math.min(Math.max(this.__currentTimeTo, 0), this.player.totalTime);
          clearTimeout(timerHandle);
          timerHandle = setTimeout(
            U.bindr(this, function () {
              this.player.currentTime = this.__currentTimeTo;
              liberator.log({
                pl: this.player.currentTime,
                to: this.__currentTimeTo
              })
              delete this.__currentTimeTo;
            }),
            1000
          );
          this.update();
        } else {
          this.player.volume += (event.detail > 0) ? -5 : 5;
          this.update();
        }
      }
    })(),

    onMutedClick: function (event) this.player.toggle('muted'),

    onPauseClick: function () this.player.pause(),

    onPlayClick: function () this.player.play(),

    onReady: function () {
      if (this.player.last.fullscreen && !this.storage.alreadyAutoFullscreen
      && !this.__autoFullscreenTimer) {
        this.__autoFullscreenTimer = setInterval(
          U.bindr(this, function () {
            if (!this.player.ready)
              return;
            clearInterval(this.__autoFullscreenTimer)
            setTimeout(
              U.bindr(this, function () (this.player.fullscreen = true)),
              this.setting.niconico.autoFullscreenDelay
            );
            delete this.__autoFullscreenTimer;
          }),
          200
        );
      }
      this.storage.alreadyAutoFullscreen = true;
    },

    onRepeatingClick: function () this.player.toggle('repeating'),

    onRelationsRootPopupshowing: function () {
      let self = this;

      function clickEvent (cmd)
        function () liberator.open(makeRelationURL(self.player, cmd));

      if (!this.player)
        return;

      let relmenu = document.getElementById('anekos-stella-relations-menupopup');
      let rels = this.player.relations;

      while (relmenu.firstChild)
        relmenu.removeChild(relmenu.firstChild);

      rels.forEach(function (rel) {
        let elem = document.createElement('menuitem');
        let prefix = rel instanceof RelatedID  ? 'ID: ' :
                     rel instanceof RelatedTag ? 'Tag: ' :
                     '';
        elem.setAttribute('label', prefix + rel.description);
        elem.addEventListener('click', clickEvent(rel.command), false);
        relmenu.appendChild(elem);
      }, this);
    },

    onResize: function () {
      if (this.__fullScreen !== window.fullScreen) {
        this.__fullScreen = window.fullScreen;
        this.onFullScreen(this.__fullScreen);
      }
    },

    onSetVolumeClick: function (event) (this.player.volume = event.target.getAttribute('volume'))
  };

  U.fixDoubleClick(Stella.prototype, 'onIconClick', 'onIconDblClick');

  // }}}

  /*********************************************************************************
  * Functions                                                                    {{{
  *********************************************************************************/

  function makeRelationURL (player, command) {
    if (!player.has('makeURL', 'x'))
      U.raise('Mysterious Error! makeURL has been not implmented.');
    if (command.match(/^[#\uff03]/))
      return player.makeURL(command.slice(1), Player.URL_ID);
    if (command.match(/^[:\uff1a]/))
      return player.makeURL(command.slice(1), Player.URL_TAG);
    if (command.indexOf('http://') == -1)
      return player.makeURL(encodeURIComponent(command), Player.URL_TAG);
    return command;
  }

  // }}}

  /*********************************************************************************
  * Install                                                                      {{{
  *********************************************************************************/

  if (InVimperator) {
    let estella = liberator.globalVariables.stella;

    let install = function () {
      let stella = liberator.globalVariables.stella = new Stella(new Setting());
      stella.addUserCommands();
      liberator.log('Stella: installed.');
    };

    // すでにインストール済みの場合は、一度ファイナライズする
    // (デバッグ時に前のパネルが残ってしまうため)
    if (estella) {
      liberator.log(estella)
      estella.finalize();
      install();
    } else {
      window.addEventListener(
        'DOMContentLoaded',
        function () {
          window.removeEventListener('DOMContentLoaded', arguments.callee, false);
          install();
        },
        false
      );
    }
  } else {
    /* do something */
  }

  // }}}

})();

// vim:sw=2 ts=2 et si fdm=marker: