root/lang/javascript/vimperator-plugins/branches/2.0/uaSwitchLite.js

Revision 29505, 5.0 kB (checked in by anekos, 19 months ago)

も少し修正

Line 
1var PLUGIN_INFO =
2<VimperatorPlugin>
3  <name>UserAgentSwitcherLite</name>
4  <description>switch user agent</description>
5  <description lang='ja'>user agent 切り替え</description>
6  <version>0.1.1</version>
7  <author homepage='http://d.hatena.ne.jp/pekepekesamurai/'>pekepeke</author>
8  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/uaSwitchLite</updateURL>
9  <minVersion>2.0pre</minVersion>
10  <maxVersion>2.0pre</maxVersion>
11  <detail lang='ja'><![CDATA[
12    == Commands ==
13      :ua [uaname]:
14        User Agent を切り替えます。
15      :ua:
16        User Agent を表示します。
17
18    == .vimperatorrrc ==
19      >||
20        javascript <<EOM
21        liberator.globalVariables.useragent_list = [
22        {
23          description: 'Internet Explorer 7 (Windows Vista)',
24          useragent: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
25          appname: 'Microsoft Internet Explorer',
26          appversion: '4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
27          platform: 'Win32',
28        }, {
29          description: 'Netscape 4.8 (Windows Vista)',
30          useragent: 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
31          appname: 'Netscape',
32          appversion: '4.8 [en] (Windows NT 6.0; U)',
33          platform: 'Win32',
34        }, {
35          description: 'Google',
36          useragent: 'Googlebot/2.1 (+http://www.google.com/bot.html)',
37        }];
38        EOM
39      ||<
40  ]]></detail>
41</VimperatorPlugin>;
42
43
44liberator.plugins.UserAgentSwitcherLite = (function(){
45
46const USER_AGENT = 'general.useragent.override';
47const APP_NAME = 'general.appname.override';
48const APP_VERSION = 'general.appversion.override';
49const PLATFORM = 'general.platform.override';
50const VENDOR = 'general.useragent.vendor';
51const VENDOR_SUB = 'general.useragent.vendorSub';
52const DEFAULT = 'Default';
53
54var global = liberator.globalVariables;
55global.useragent_list = global.useragent_list ? global.useragent_list : [
56  {
57    description: 'Internet Explorer 7 (Windows Vista)',
58    useragent: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
59    appname: 'Microsoft Internet Explorer',
60    appversion: '4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
61    platform: 'Win32',
62    vendor: '',
63    vendorSub: ''
64  }, {
65    description: 'Netscape 4.8 (Windows Vista)',
66    useragent: 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
67    appname: 'Netscape',
68    appversion: '4.8 [en] (Windows NT 6.0; U)',
69    platform: 'Win32',
70    vendor: '',
71    vendorSub: ''
72  }, {
73    description: 'Opera 9.25 (Windows Vista)',
74    useragent: 'Opera/9.25 (Windows NT 6.0; U; en)',
75    appname: 'Opera',
76    appversion: '9.25 (Windows NT 6.0; U; en)',
77    platform: 'Win32',
78  }
79];
80
81var Class = function() function(){ this.initialize.apply(this, arguments); };
82
83var UASwitcherLite = new Class();
84UASwitcherLite.prototype = {
85  initialize: function(){
86    // init
87    this.completer = [];
88    this.switcher = {
89      __noSuchMethod__: function(arg) liberator.echoerr('cannot switch user agent "'+arg+'"'),
90      '': function(){
91        var ua = options.getPref(USER_AGENT);
92        liberator.echo('Current User Agent : ' + (ua ? ua : DEFAULT) );
93      }
94    };
95    var self = this;
96
97    // default values
98    this.completer.push([DEFAULT, '']);
99    this.switcher[DEFAULT] = function() self.switch();
100
101    // expand setting
102    global.useragent_list.forEach( function(item){
103      var desc = item.description;
104      var userAgent = item.useragent;
105      var appName = item.appname;
106      var appVersion = item.appversion;
107      var platform = item.platform;
108      var vendor = item.vendor;
109      var vendorSub = item.vendorSub;
110      self.completer.push([desc, userAgent]);
111      self.switcher[desc] = function() self.switch(appName, appVersion, platform, userAgent, vendor, vendorSub);
112    });
113    this.registerCommand();
114  },
115  switch: function(appName, appVersion, platform, userAgent, vendor, vendorSub){
116    if (!userAgent && !options.getPref(USER_AGENT)) return;
117    var setter = userAgent ? options.setPref : options.resetPref;
118    setter(APP_NAME, decodeURIComponent(appName || ''));
119    setter(APP_VERSION, decodeURIComponent(appVersion || ''));
120    setter(PLATFORM, decodeURIComponent(platform || ''));
121    setter(USER_AGENT, decodeURIComponent(userAgent || ''));
122    setter(VENDOR, decodeURIComponent(vendor || ''));
123    setter(VENDOR_SUB, decodeURIComponent(vendorSub || ''));
124  },
125  registerCommand: function(){
126    var self = this;
127    commands.addUserCommand(['ua'], 'Switch User Agent',
128      function(arg)
129        self.switcher[ (arg.string || arg+'').replace(/\\+/g,'') ](),
130      {
131        completer: function(context, args, special){
132          var filter = context.filter;
133          context.title = ['Description', 'User Agent'];
134          if (!filter) {
135            context.completions = self.completer;
136            return;
137          }
138          filter = filter.toLowerCase();
139          context.completions = self.completer.filter( function(el) el[0].toLowerCase().indexOf(filter) == 0 );
140        }
141    } );
142  }
143};
144return new UASwitcherLite();
145})();
Note: See TracBrowser for help on using the browser.