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();
})();
;ApiKey=" + info.apiKey; liberator.log("POST DATA:" + postBody); var req = new libly.Request( endpoint, null, { asyncronus: false, postBody: postBody } ); req.addEventListener("onSuccess", function(data) { liberator.log("Post status: " + data.responseText); liberator.echo("Subscribe feed succeed."); }); req.addEventListener("onFailure", function(data) { liberator.log("POST FAILURE: " + data.responseText); liberator.echoerr("POST FAILURE: " + data.statusText); }); req.post(); } commands.addUserCommand( command, "Register feed subscriptions to " + servicename + ".", function(args) { try { handleFeedRequest({rate: args["-rate"]}); } catch (e) { liberator.echoerr(e); } }, { options: [ [["-rate", "-r"], commands.OPTION_INT] ] }, true // Use in DEVELOP ); } function getSubscribeInfo(htmldoc) { var subscribeInfo = { target_url: null, register: 1, apiKey: null, feedlinks: [] }; $LXs('id("feed_candidates")/xhtml:li', htmldoc).forEach( function(item) { var feedlink = $LX('./xhtml:a[@class="feedlink"]', item); var title = $LX('./xhtml:a[@class="subscribe_list"]', item); var users = $LX('./xhtml:span[@class="subscriber_count"]/xhtml:a', item); var yet = $LX('./xhtml:input[@name="feedlink"]', item); liberator.log("input:" + feedlink.href); subscribeInfo.feedlinks.push([feedlink.href, (yet != null), (title ? title.textContent : '' ) + ' / ' + (users ? users.textContent : '0 user')]); }); var target_url = $LX('id("target_url")', htmldoc); if (!target_url) throw "Cannot find subscribe info about this page!"; subscribeInfo.target_url = target_url.value; liberator.log("target_url:" + subscribeInfo.target_url); subscribeInfo.apiKey = $LX('//*[@name="ApiKey"]', htmldoc).value; if (!subscribeInfo.apiKey) throw "Can't get API key for subscription!"; return subscribeInfo; } function selectFeed(links, next) { liberator.log(links.toSource()); liberator.echo("Following feeds were found on this site. Which are you subscribe?"); commandline.input("Select or input feed URL ", function(selected) { liberator.echo("You select " + selected + "."); commandline.close(); if (next && typeof next == "function") next(selected); else liberator.echoerr("Your selected no is invalid."); },{ completer: function(context) { context.title = ["Available feeds", "Title / users"]; context.completions = links; } }); // Open candidates list forcibly events.feedkeys("<TAB>"); } // For convenience //function $LXs(a,b) libly.$U.getNodesFromXPath(a,b); //function $LX(a,b) libly.$U.getFirstNodeFromXPath(a,b); function nsResolver(prefix) { var ns = { 'xhtml': 'http://www.w3.org/1999/xhtml' }; return ns[prefix] || null; } function $LXs(a, b) { var ret = []; var res = (b.ownerDocument || b).evaluate(a, b, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); for (var i = 0; i < res.snapshotLength; i++) { ret.push(res.snapshotItem(i)); } return ret; } function $LX(a, b) { var res = (b.ownerDocument || b).evaluate(a, b, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null); return res.singleNodeValue || null; } // }}} return PUBLICS; })(); // vim:sw=2 ts=2 et si fdm=marker: