1. var twitter = {};
  2. var accounts = store.get("accounts") || [];
  3. twitter.version = 4;
  4. twitter.consumerKey = "JPRtaafuUN3zcqFjxXCbJQ";
  5. twitter.consumerSecret = "VOT7yrQ5AZ57QzqljTRRjwylf5KPUfWoUujibZMEw";
  6. twitter.growlId = "bmnmjejkpblabmgiehpgjpiglmnhocoi";
  7. twitter.setup = function () {
  8.     localStorage.removeItem("timelineCache");
  9.     var a = $.get("https://api.twitter.com/1/help/test.json", function (c) {
  10.         OAuth.correctTimestamp(new Date(a.getResponseHeader("Date")).getTime() / 1000);
  11.         for (var b in accounts) {
  12.             if (!accounts[b].accessToken) {
  13.                 accounts.splice(b, 1);
  14.                 updateAccounts();
  15.                 document.location.reload()
  16.             } else {
  17.                 if (accounts.length == 1) {
  18.                     accounts[b]["default"] = true
  19.                 }
  20.                 twitter.loadAccount(accounts[b])
  21.             }
  22.         }
  23.     });
  24.     twitter.messagesDB = window.webkitIndexedDB.open("messagesDB");
  25.     twitter.messagesDB.onsuccess = function () {
  26.         twitter.messagesDB = twitter.messagesDB.result;
  27.         if (twitter.messagesDB.version == "") {
  28.             var b = twitter.messagesDB.setVersion(1);
  29.             b.onsuccess = function (f) {
  30.                 for (var d in accounts) {
  31.                     var c = twitter.messagesDB.createObjectStore(accounts[d].id, {
  32.                         keyPath: "id_str"
  33.                     });
  34.                     c.createIndex("sender_id", "sender_id", {
  35.                         unique: false
  36.                     });
  37.                     c.createIndex("recipient_id", "recipient_id", {
  38.                         unique: false
  39.                     })
  40.                 }
  41.             }
  42.         }
  43.     }
  44. };
  45. twitter.oauthRequest = function (d) {
  46.     accessor = {
  47.         consumerSecret: twitter.consumerSecret
  48.     };
  49.     message = {
  50.         action: d.url,
  51.         method: d.method || "GET",
  52.         parameters: [
  53.             ["oauth_consumer_key", twitter.consumerKey],
  54.             ["oauth_signature_method", "HMAC-SHA1"],
  55.             ["oauth_version", "1.0"]
  56.         ]
  57.     };
  58.     d.token = d.token || d.account.accessToken;
  59.     d.tokenSecret = d.tokenSecret || d.account.accessTokenSecret;
  60.     if (d.token != true) {
  61.         message.parameters.push(["oauth_token", d.token])
  62.     }
  63.     if (d.tokenSecret != true) {
  64.         accessor.tokenSecret = d.tokenSecret
  65.     }
  66.     for (var a in d.parameters) {
  67.         if (d.parameters[a][0] == "oauth_callback") {
  68.             d.parameters[a][1] = unescape(d.parameters[a][1])
  69.         }
  70.         message.parameters.push(d.parameters[a])
  71.     }
  72.     OAuth.setTimestampAndNonce(message);
  73.     OAuth.SignatureMethod.sign(message, accessor);
  74.     if (d.stream) {
  75.         console.log("Opening user stream for user " + d.account.screenName);
  76.         $.ajax({
  77.             url: message.action,
  78.             type: message.method,
  79.             data: OAuth.getParameterMap(message.parameters),
  80.             dataType: d.type || "json",
  81.             xhr: function () {
  82.                 d.account.stream.connection = new XMLHttpRequest();
  83.                 d.account.stream.connection.addEventListener("progress", function () {
  84.                     processResponse(d.account)
  85.                 }, false);
  86.                 return d.account.stream.connection
  87.             },
  88.             complete: function (f, g) {
  89.                 streamComplete(f.status, d.account)
  90.             }
  91.         });
  92.         setInterval(function () {
  93.             checkStream(d.account)
  94.         }, 45000)
  95.     } else {
  96.         var e = true;
  97.         if (d.media) {
  98.             e = false;
  99.             var c = new FormData();
  100.             c.append("media[]", d.media);
  101.             c.append("status", d.status);
  102.             if (d.replyID) {
  103.                 c.append("in_reply_to_status_id", d.replyID)
  104.             }
  105.             var b = function (f) {
  106.                     f.setRequestHeader("Authorization", OAuth.getAuthorizationHeader("", message.parameters))
  107.                 }
  108.         }
  109.         $.ajax({
  110.             url: message.action,
  111.             type: message.method,
  112.             data: c || OAuth.getParameterMap(message.parameters),
  113.             dataType: d.type || "json",
  114.             contentType: (d.media) ? false : "application/x-www-form-urlencoded",
  115.             processData: e,
  116.             beforeSend: b ||
  117.             function () {},
  118.             xhr: d.xhr,
  119.             success: function (g, f, i) {
  120.                 if (d.success) {
  121.                     d.success(g)
  122.                 }
  123.             },
  124.             error: function (g, f, i) {
  125.                 console.log(g.responseText);
  126.                 if (d.error) {
  127.                     d.error(g.responseText, d.parameters, g)
  128.                 }
  129.             }
  130.         })
  131.     }
  132. };
  133.  
  134. function processResponse(f) {
  135.     f.stream.response = f.stream.connection.responseText.split("\r");
  136.     while (f.stream.response[f.stream.index + 1]) {
  137.         if (f.stream.response[f.stream.index].length > 1) {
  138.             var e;
  139.             if (e = jQuery.parseJSON(f.stream.response[f.stream.index])) {
  140.                 if (e.text) {
  141.                     f.stream.timeline.unshift(e);
  142.                     if (e.entities.user_mentions) {
  143.                         var b = false;
  144.                         for (var c in e.entities.user_mentions) {
  145.                             if (e.entities.user_mentions[c].screen_name == f.screenName) {
  146.                                 b = true;
  147.                                 f.stream.mentions.unshift(e);
  148.                                 var d = false;
  149.                                 var c = 0;
  150.                                 while (f.stream.friends[c] && !d) {
  151.                                     if (f.stream.friends[c] == e.user.id) {
  152.                                         d = true
  153.                                     }
  154.                                     c++
  155.                                 }
  156.                                 if (!d) {
  157.                                     f.stream.timeline.splice(0, 1)
  158.                                 }
  159.                             }
  160.                         }
  161.                     }
  162.                     if (e.user.id == f.id) {
  163.                         f.stream.userTimeline.unshift(e)
  164.                     }
  165.                     if (f.notifyTweet || (f.notifyMention && b)) {
  166.                         twitter.notify(f, f.stream.index)
  167.                     }
  168.                     while (f.stream.timeline.length > 100) {
  169.                         f.stream.timeline.pop()
  170.                     }
  171.                     while (f.stream.mentions.length > 100) {
  172.                         f.stream.mentions.pop()
  173.                     }
  174.                     while (f.stream.userTimeline.length > 100) {
  175.                         f.stream.mentions.pop()
  176.                     }
  177.                     var a = chrome.extension.getViews({});
  178.                     for (var c in a) {
  179.                         if (a[c].updateTimeline) {
  180.                             a[c].updateTimeline("home", f);
  181.                             if (b) {
  182.                                 a[c].updateTimeline("mentions", f)
  183.                             }
  184.                             if (e.user.id == f.id) {
  185.                                 a[c].updateTimeline("userTimeline", f)
  186.                             }
  187.                         }
  188.                     }
  189.                 } else {
  190.                     if (e.direct_message) {
  191.                         twitter.messagesDB.transaction([f.id], webkitIDBTransaction.READ_WRITE).objectStore(f.id).add(e.direct_message);
  192.                         if (f.notifyDm) {
  193.                             twitter.notify(f, f.stream.index)
  194.                         }
  195.                         var a = chrome.extension.getViews({});
  196.                         for (var c in a) {
  197.                             if (a[c].updateMessage) {
  198.                                 a[c].updateMessage(f.id, e.direct_message)
  199.                             }
  200.                         }
  201.                     } else {
  202.                         if (e["delete"]) {
  203.                             var c = 0;
  204.                             if (f.stream.timeline[c]) {
  205.                                 while (f.stream.timeline[c].id_str > e["delete"].status.id_str || f.stream.timeline[c].retweeted_by && f.stream.timeline[c].id_str != e["delete"].status.id_str) {
  206.                                     c++;
  207.                                     if (!f.stream.timeline[c]) {
  208.                                         break
  209.                                     }
  210.                                 }
  211.                             }
  212.                             if (f.stream.timeline[c]) {
  213.                                 if (f.stream.timeline[c].id_str == e["delete"].status.id_str) {
  214.                                     f.stream.timeline.splice(c, 1)
  215.                                 }
  216.                             }
  217.                             var a = chrome.extension.getViews({});
  218.                             for (var c in a) {
  219.                                 if (a[c].deleteStatus) {
  220.                                     a[c].deleteStatus(f.id, e["delete"].status.id_str)
  221.                                 }
  222.                             }
  223.                         } else {
  224.                             if (e.friends) {
  225.                                 f.stream.friends = e.friends
  226.                             }
  227.                         }
  228.                     }
  229.                 }
  230.             } else {
  231.                 f.stream.connection.abort()
  232.             }
  233.         }
  234.         f.stream.index++
  235.     }
  236. }
  237. function checkStream(a) {
  238.     console.log("Checking stream activity...");
  239.     if (a.stream.connection) {
  240.         if (a.stream.connection.responseText.length > 5000000 || a.stream.lastIndex == a.stream.index) {
  241.             a.stream.connection.abort()
  242.         }
  243.         a.stream.lastIndex = a.stream.index
  244.     }
  245. }
  246. function updateAccounts() {
  247.     var a = [];
  248.     for (var b in accounts) {
  249.         a[b] = jQuery.extend({}, accounts[b]);
  250.         delete a[b].stream
  251.     }
  252.     if (a.length == 0) {
  253.         store.set("accounts", "")
  254.     } else {
  255.         store.set("accounts", a)
  256.     }
  257. }
  258. twitter.stream = function (a) {
  259.     this.response = "";
  260.     this.timeline = [];
  261.     this.mentions = [];
  262.     this.userTimeline = [];
  263.     this.connection = "";
  264.     this.index = 0;
  265.     this.lastIndex = 0;
  266.     this.wait = a || 0;
  267.     this.start = function (e) {
  268.         console.log("Fetching timelines...");
  269.         twitter.getTimeline({
  270.             accountId: e.id,
  271.             type: "home",
  272.             success: function (f) {
  273.                 e.stream.timeline = f;
  274.                 console.log("Fetched home")
  275.             }
  276.         });
  277.         twitter.getUserTimeline({
  278.             accountId: e.id,
  279.             success: function (f) {
  280.                 e.stream.userTimeline = f;
  281.                 console.log("Fetched user")
  282.             }
  283.         });
  284.         twitter.getMentions({
  285.             accountId: e.id,
  286.             success: function (f) {
  287.                 e.stream.mentions = f;
  288.                 console.log("Fetched mentions")
  289.             }
  290.         });
  291.         var c = 0;
  292.         while (twitter.messagesDB.objectStoreNames[c] && twitter.messagesDB.objectStoreNames[c] != e.id) {
  293.             c++
  294.         }
  295.         function b(f) {
  296.             twitter.messagesDB.transaction([f]).objectStore(f).openCursor(null, webkitIDBCursor.PREV).onsuccess = function (j) {
  297.                 console.log("Loaded messages DB cursor");
  298.                 var k = j.target.result;
  299.                 if (k) {
  300.                     var i = k.key;
  301.                     var g = [
  302.                         ["since_id", twitter.decrementID(k.key)]
  303.                     ]
  304.                 }
  305.                 if (!i) {
  306.                     var g = [
  307.                         ["count", 200]
  308.                     ]
  309.                 }
  310.                 twitter.getMessages({
  311.                     accountId: f,
  312.                     parameters: g,
  313.                     success: function (n) {
  314.                         var m = twitter.messagesDB.transaction([e.id], webkitIDBTransaction.READ_WRITE).objectStore(e.id);
  315.                         var l = function () {
  316.                                 for (var o in n) {
  317.                                     m.add(n[o])
  318.                                 }
  319.                             };
  320.                         if (n.length) {
  321.                             if (n[n.length - 1].id_str != i) {
  322.                                 m.openCursor().onsuccess = function (o) {
  323.                                     var p = o.target.result;
  324.                                     if (p) {
  325.                                         p["delete"]();
  326.                                         p["continue"]()
  327.                                     } else {
  328.                                         l()
  329.                                     }
  330.                                 }
  331.                             } else {
  332.                                 n.pop();
  333.                                 l()
  334.                             }
  335.                         }
  336.                         console.log("Fetched messages")
  337.                     }
  338.                 })
  339.             }
  340.         }
  341.         if (!twitter.messagesDB.objectStoreNames[c]) {
  342.             console.log("Creating object store");
  343.             var d = twitter.messagesDB.setVersion(parseInt(twitter.messagesDB.version) + 1).onsuccess = function (g) {
  344.                     var f = chrome.extension.getBackgroundPage().twitter.messagesDB.createObjectStore(e.id, {
  345.                         keyPath: "id_str"
  346.                     });
  347.                     f.createIndex("sender_id", "sender_id", {
  348.                         unique: false
  349.                     });
  350.                     f.createIndex("recipient_id", "recipient_id", {
  351.                         unique: false
  352.                     });
  353.                     b(e.id)
  354.                 }
  355.         } else {
  356.             b(e.id)
  357.         }
  358.         twitter.oauthRequest({
  359.             url: "https://userstream.twitter.com/2/user.json",
  360.             stream: true,
  361.             account: e
  362.         })
  363.     }
  364. };
  365.  
  366. function streamComplete(a, b) {
  367.     console.log(b.screenName + " stream complete");
  368.     delete b.stream.connection;
  369.     if (!b.disabled) {
  370.         if (a > 200) {
  371.             if (b.stream.wait < 240000) {
  372.                 setTimeout(function () {
  373.                     b.stream = new twitter.stream(b.stream.wait);
  374.                     b.stream.start(b)
  375.                 }, b.stream.wait);
  376.                 if (b.stream.wait == 0) {
  377.                     b.stream.wait = 5000
  378.                 }
  379.                 b.stream.wait *= 2
  380.             } else {
  381.                 notif = webkitNotifications.createNotification("images/48.png", "Twitter Error", "Twitter Notifier couldn't connect to Twitter - maybe Twitter's down, or maybe you aren't logged in properly. Go to the options page to logout and in again.");
  382.                 notif.show();
  383.                 setTimeout(function () {
  384.                     b.stream = new twitter.stream(b.stream.wait);
  385.                     b.stream.start(b)
  386.                 }, 10000)
  387.             }
  388.         } else {
  389.             if (b.stream.wait < 30000) {
  390.                 setTimeout(function () {
  391.                     b.stream = new twitter.stream(b.stream.wait);
  392.                     b.stream.start(b)
  393.                 }, b.stream.wait);
  394.                 b.stream.wait += 10000
  395.             } else {
  396.                 connectionError()
  397.             }
  398.         }
  399.         b.waitReset = setTimeout(function () {
  400.             b.wait = 0
  401.         }, 320000)
  402.     }
  403. }
  404. function connectionError() {
  405.     var a = webkitNotifications.createNotification("images/48.png", "Connection Error", "Twitter Notifier couldn't connect to Twitter - are you sure you're connected to the internet? Connection will be tried again in 1 minute.");
  406.     if (store.get("connectionErrors")) {
  407.         a.show()
  408.     }
  409.     setTimeout(function () {
  410.         a.cancel();
  411.         document.location.reload()
  412.     }, 60000)
  413. }
  414. twitter.requestToken = function () {
  415.     var a = Date.now();
  416.     twitter.oauthRequest({
  417.         url: "https://api.twitter.com/oauth/request_token",
  418.         type: "text",
  419.         method: "POST",
  420.         token: true,
  421.         tokenSecret: true,
  422.         parameters: [
  423.             ["oauth_callback", escape(window.top.location + "?t=" + a)]
  424.         ],
  425.         success: function (d) {
  426.             d = d.split("&");
  427.             var b = [];
  428.             for (var c in d) {
  429.                 var e = d[c].split("=");
  430.                 b[e[0]] = e[1]
  431.             }
  432.             accounts.push({
  433.                 requestToken: b.oauth_token,
  434.                 requestTokenSecret: b.oauth_token_secret,
  435.                 timestamp: a
  436.             });
  437.             updateAccounts();
  438.             if (b.oauth_token) {
  439.                 chrome.tabs.create({
  440.                     url: "https://api.twitter.com/oauth/authorize?oauth_token=" + b.oauth_token
  441.                 })
  442.             } else {
  443.                 setTimeout(function () {
  444.                     alert("An error occurred! Please check you have internet access and your clock is set correctly and try again.")
  445.                 }, 5000)
  446.             }
  447.         },
  448.         error: function (d, c) {
  449.             webkitNotifications.createNotification("images/error.png", "Failed to authenticate with Twitter", "Check you have internet access and your computer's clock is set correctly (common cause of error) and try again.").show()
  450.         }
  451.     })
  452. };
  453. twitter.accessToken = function (a, e) {
  454.     accounts = chrome.extension.getBackgroundPage().accounts;
  455.     for (var b in accounts) {
  456.         if (accounts[b].timestamp == e) {
  457.             var d = accounts[b];
  458.             var c = b
  459.         }
  460.     }
  461.     twitter.oauthRequest({
  462.         url: "https://api.twitter.com/oauth/access_token",
  463.         type: "text",
  464.         method: "POST",
  465.         token: d.requestToken,
  466.         tokenSecret: d.requestTokenSecret,
  467.         parameters: [
  468.             ["oauth_verifier", a]
  469.         ],
  470.         success: function (j) {
  471.             j = j.split("&");
  472.             var f = [];
  473.             for (var g in j) {
  474.                 var k = j[g].split("=");
  475.                 f[k[0]] = k[1]
  476.             }
  477.             for (var g in accounts) {
  478.                 if (accounts[g].accessToken == f.oauth_token) {
  479.                     accounts.splice(c, 1);
  480.                     c = false
  481.                 }
  482.             }
  483.             if (c) {
  484.                 delete d.timestamp;
  485.                 delete d.requestToken;
  486.                 delete d.requestTokenSecret;
  487.                 d.accessToken = f.oauth_token;
  488.                 d.accessTokenSecret = f.oauth_token_secret;
  489.                 d.notifyDm = true;
  490.                 d.notifyTweet = true;
  491.                 d.notifyMention = true;
  492.                 chrome.extension.getBackgroundPage().twitter.loadNewAccount(d);
  493.                 webkitNotifications.createNotification("images/48.png", "Authorised!", "Twitter Notifier is now authorised and running.").show()
  494.             } else {
  495.                 webkitNotifications.createNotification("images/48.png", "Not Authorised", "That account has already been connected to Twitter Notifier.").show()
  496.             }
  497.             updateAccounts();
  498.             chrome.tabs.getCurrent(function (i) {
  499.                 chrome.tabs.remove(i.id)
  500.             })
  501.         }
  502.     })
  503. };
  504. twitter.loadAccount = function (a) {
  505.     console.log("Loading account " + a.screenName);
  506.     twitter.oauthRequest({
  507.         url: "https://api.twitter.com/1/account/verify_credentials.json",
  508.         account: a,
  509.         success: function (c) {
  510.             try {
  511.                 a.id = c.id;
  512.                 a.name = c.name;
  513.                 a.screenName = c.screen_name;
  514.                 a.image = c.profile_image_url;
  515.                 a.stream = new twitter.stream();
  516.                 console.log("Updating accounts store");
  517.                 updateAccounts();
  518.                 if (!a.disabled) {
  519.                     a.stream.start(a)
  520.                 }
  521.             } catch (b) {
  522.                 connectionError()
  523.             }
  524.         },
  525.         error: function (c, b) {
  526.             webkitNotifications.createNotification("images/48.png", "Could not authenticate user @" + a.screenName, "Twitter Notifier failed to authorise this account with Twitter. Try logging out and logging in of this account on the accounts page.").show()
  527.         }
  528.     })
  529. };
  530. twitter.loadNewAccount = function (b) {
  531.     for (var a in accounts) {
  532.         if (accounts[a].accessToken == b.accessToken) {
  533.             twitter.loadAccount(accounts[a])
  534.         }
  535.     }
  536. };
  537. twitter.getAccountById = function (b) {
  538.     for (var a in accounts) {
  539.         if (accounts[a].id == b) {
  540.             return accounts[a]
  541.         }
  542.     }
  543. };
  544. twitter.favourite = function (d, c, b) {
  545.     var a = twitter.getAccountById(c);
  546.     twitter.oauthRequest({
  547.         method: "POST",
  548.         url: "https://api.twitter.com/1/favorites/create/" + d + ".json",
  549.         account: a,
  550.         success: b ||
  551.         function () {},
  552.         error: function (e) {
  553.             e = jQuery.parseJSON(e);
  554.             webkitNotifications.createNotification("images/error.png", "Could not favourite", "Error: " + e.error).show()
  555.         }
  556.     })
  557. };
  558. twitter.unfavourite = function (d, c, b) {
  559.     var a = twitter.getAccountById(c);
  560.     twitter.oauthRequest({
  561.         method: "POST",
  562.         url: "https://api.twitter.com/1/favorites/destroy/" + d + ".json",
  563.         account: a,
  564.         success: b ||
  565.         function () {},
  566.         error: function (e) {
  567.             e = jQuery.parseJSON(e);
  568.             webkitNotifications.createNotification("images/error.png", "Could not unfavourite", "Error: " + e.error).show()
  569.         }
  570.     })
  571. };
  572. twitter.retweet = function (d, c, b) {
  573.     var a = twitter.getAccountById(c);
  574.     twitter.oauthRequest({
  575.         method: "POST",
  576.         url: "https://api.twitter.com/1/statuses/retweet/" + d + ".json",
  577.         account: a,
  578.         success: b ||
  579.         function () {},
  580.         error: function (e) {
  581.             e = jQuery.parseJSON(e);
  582.             webkitNotifications.createNotification("images/error.png", "Retweet failed", "Error: " + e.error).show()
  583.         }
  584.     })
  585. };
  586. twitter.deleteStatus = function (d, c, b) {
  587.     var a = twitter.getAccountById(c);
  588.     twitter.oauthRequest({
  589.         method: "POST",
  590.         url: "https://api.twitter.com/1/statuses/destroy/" + d + ".json",
  591.         account: a,
  592.         success: b ||
  593.         function () {},
  594.         error: function (e) {
  595.             e = jQuery.parseJSON(e);
  596.             webkitNotifications.createNotification("images/error.png", "Retweet failed", "Error: " + e.error).show()
  597.         }
  598.     })
  599. };
  600. twitter.update = function (f, c, e, d) {
  601.     var b = twitter.getAccountById(f);
  602.     var a = [
  603.         ["status", c]
  604.     ];
  605.     if (e) {
  606.         a.push(["in_reply_to_status_id", e])
  607.     }
  608.     twitter.oauthRequest({
  609.         method: "POST",
  610.         url: "https://api.twitter.com/1/statuses/update.json",
  611.         parameters: a,
  612.         account: b,
  613.         success: d ||
  614.         function () {},
  615.         error: function (g, k) {
  616.             g = jQuery.parseJSON(g);
  617.             console.log(g);
  618.             webkitNotifications.createNotification("images/error.png", "Status could not be sent", "Error: " + g.error).show();
  619.             for (var j in k) {
  620.                 if (k[j][0] == "status") {
  621.                     k = k[j][1]
  622.                 }
  623.             }
  624.             chrome.tabs.create({
  625.                 url: "http://twitter.com/?status=" + k
  626.             })
  627.         }
  628.     })
  629. };
  630. twitter.updateWithMedia = function (i, e, d, g, f, a) {
  631.     var c = twitter.getAccountById(i);
  632.     var b = [];
  633.     twitter.oauthRequest({
  634.         method: "POST",
  635.         url: "https://upload.twitter.com/1/statuses/update_with_media.json",
  636.         parameters: b,
  637.         account: c,
  638.         media: d,
  639.         status: e,
  640.         replyID: g || false,
  641.         xhr: function () {
  642.             var j = new XMLHttpRequest();
  643.             j.upload.addEventListener("progress", a ||
  644.             function () {}, false);
  645.             return j
  646.         },
  647.         success: f ||
  648.         function () {},
  649.         error: function (j, l) {
  650.             j = jQuery.parseJSON(j);
  651.             console.log(j);
  652.             webkitNotifications.createNotification("images/error.png", "Status could not be sent", "Error: " + j.error).show();
  653.             for (var k in l) {
  654.                 if (l[k][0] == "status") {
  655.                     l = l[k][1]
  656.                 }
  657.             }
  658.             chrome.tabs.create({
  659.                 url: "http://twitter.com/?status=" + l
  660.             })
  661.         }
  662.     })
  663. };
  664. twitter.dm = function (e, b, d, c) {
  665.     var a = twitter.getAccountById(e);
  666.     twitter.oauthRequest({
  667.         method: "POST",
  668.         url: "https://api.twitter.com/1/direct_messages/new.json",
  669.         parameters: [
  670.             ["text", b],
  671.             ["user_id", d]
  672.         ],
  673.         account: a,
  674.         error: function (f) {
  675.             webkitNotifications.createNotification("images/error.png", "DM Could not be sent", "Error: " + jQuery.parseJSON(f)["error"]).show()
  676.         },
  677.         success: c ||
  678.         function () {}
  679.     })
  680. };
  681. twitter.getTimeline = function (c) {
  682.     var b = twitter.getAccountById(c.accountId);
  683.     var a = [
  684.         ["include_rts", "true"],
  685.         ["include_entities", "true"],
  686.         ["include_my_retweet", "true"]
  687.     ];
  688.     if (c.parameters) {
  689.         a = a.concat(c.parameters)
  690.     }
  691.     twitter.oauthRequest({
  692.         method: "GET",
  693.         url: "https://api.twitter.com/1/statuses/home_timeline.json",
  694.         account: b,
  695.         parameters: a,
  696.         success: c.success ||
  697.         function () {},
  698.         error: c.error ||
  699.         function () {}
  700.     })
  701. };
  702. twitter.getUserTimeline = function (c) {
  703.     var b = twitter.getAccountById(c.accountId);
  704.     var a = [
  705.         ["include_rts", "true"],
  706.         ["include_entities", "true"],
  707.         ["include_my_retweet", "true"]
  708.     ];
  709.     if (c.parameters) {
  710.         a = a.concat(c.parameters)
  711.     }
  712.     twitter.oauthRequest({
  713.         method: "GET",
  714.         url: "https://api.twitter.com/1/statuses/user_timeline.json",
  715.         account: b,
  716.         parameters: a,
  717.         success: c.success ||
  718.         function () {},
  719.         error: c.error ||
  720.         function () {}
  721.     })
  722. };
  723. twitter.getMentions = function (c) {
  724.     var b = twitter.getAccountById(c.accountId);
  725.     var a = [
  726.         ["include_rts", "true"],
  727.         ["include_entities", "true"]
  728.     ];
  729.     if (c.parameters) {
  730.         a = a.concat(c.parameters)
  731.     }
  732.     twitter.oauthRequest({
  733.         method: "GET",
  734.         url: "https://api.twitter.com/1/statuses/mentions.json",
  735.         account: b,
  736.         parameters: a,
  737.         success: c.success ||
  738.         function () {},
  739.         error: c.error ||
  740.         function () {}
  741.     })
  742. };
  743. twitter.getMessages = function (d) {
  744.     var c = twitter.getAccountById(d.accountId);
  745.     var b = [
  746.         ["skip_status", "true"]
  747.     ];
  748.     if (d.parameters) {
  749.         b = b.concat(d.parameters)
  750.     }
  751.     if (b.sent) {
  752.         var a = "https://api.twitter.com/1/direct_messages/sent.json";
  753.         delete b.sent
  754.     }
  755.     twitter.oauthRequest({
  756.         method: "GET",
  757.         url: "https://api.twitter.com/1/direct_messages.json",
  758.         account: c,
  759.         parameters: b,
  760.         success: function (e) {
  761.             twitter.oauthRequest({
  762.                 method: "GET",
  763.                 url: "https://api.twitter.com/1/direct_messages/sent.json",
  764.                 account: c,
  765.                 parameters: b,
  766.                 success: function (f) {
  767.                     e = e.concat(f);
  768.                     e.sort(function (i, g) {
  769.                         return new Date(g.created_at) - new Date(i.created_at)
  770.                     });
  771.                     d.success(e);
  772.                     console.log(e)
  773.                 },
  774.                 error: d.error ||
  775.                 function () {}
  776.             })
  777.         },
  778.         error: d.error ||
  779.         function () {}
  780.     })
  781. };
  782. twitter.getUsers = function (b) {
  783.     var a = twitter.getAccountById(b.accountId);
  784.     twitter.oauthRequest({
  785.         method: "POST",
  786.         url: "https://api.twitter.com/1/users/lookup.json",
  787.         account: a,
  788.         parameters: b.parameters,
  789.         success: b.success ||
  790.         function () {},
  791.         error: b.error ||
  792.         function () {}
  793.     })
  794. };
  795. twitter.getFriendships = function (b) {
  796.     var a = twitter.getAccountById(b.accountId);
  797.     twitter.oauthRequest({
  798.         method: "GET",
  799.         url: "https://api.twitter.com/1/friendships/show.json",
  800.         account: a,
  801.         parameters: b.parameters,
  802.         success: b.success ||
  803.         function () {},
  804.         error: b.error ||
  805.         function () {}
  806.     })
  807. };
  808. twitter.openReplyWindow = function (a) {
  809.     if (navigator.appVersion.indexOf("Mac") != -1) {
  810.         w = 449;
  811.         h = 90
  812.     } else {
  813.         w = 463;
  814.         h = 126
  815.     }
  816.     chrome.windows.create({
  817.         url: "reply.html" + a,
  818.         width: w,
  819.         height: h,
  820.         type: "popup",
  821.         focused: true
  822.     })
  823. };
  824. twitter.notify = function (b, a) {
  825.     var c = {};
  826.     if (store.get("growl")) {
  827.         var d = jQuery.parseJSON(b.stream.response[a]);
  828.         if (d.direct_message) {
  829.             if (d.direct_message.sender.id == b.id) {
  830.                 c.title = "Message to " + d.direct_message.recipient.name
  831.             } else {
  832.                 c.title = "Message from " + d.direct_message.sender.name
  833.             }
  834.             c.text = $("<div/>").html(d.direct_message.text).text();
  835.             c.url = "http://twitter.com/messages";
  836.             c.image = d.direct_message.sender.profile_image_url
  837.         } else {
  838.             if (d.retweeted_status) {
  839.                 d = d.retweeted_status
  840.             }
  841.             c.title = d.user.name;
  842.             c.text = $("<div/>").html(d.text).text();
  843.             c.url = "http://twitter.com/" + d.user.screen_name + "/status/" + d.id_str;
  844.             c.image = d.user.profile_image_url
  845.         }
  846.         chrome.extension.sendRequest(twitter.growlId, c, function (e) {
  847.             webkitNotifications.createHTMLNotification("notification.html?account=" + b.id + "&id=" + a).show()
  848.         })
  849.     } else {
  850.         webkitNotifications.createHTMLNotification("notification.html?account=" + b.id + "&id=" + a).show()
  851.     }
  852. };
  853. twitter.decrementID = function (b) {
  854.     var a = -1;
  855.     while (b.substr(a, 1) == "0") {
  856.         a--
  857.     }
  858.     return b.slice(0, a) + (b.substr(a) - 1)
  859. };
  860.  
  861. function removeObjectStore(b) {
  862.     var a = twitter.messagesDB.setVersion(parseInt(twitter.messagesDB.version) + 1);
  863.     a.onsuccess = function (c) {
  864.         twitter.messagesDB.deleteObjectStore(b)
  865.     }
  866. }
  867. $(document).ready(function () {
  868.     var d = window.location.href.split("?");
  869.     if (d[1]) {
  870.         d = d[1].split("&") || null;
  871.         for (var b in d) {
  872.             d[b] = d[b].split("=");
  873.             if (d[b][0] == "oauth_verifier") {
  874.                 var a = d[b][1]
  875.             }
  876.             if (d[b][0] == "t") {
  877.                 var c = d[b][1]
  878.             }
  879.         }
  880.     }
  881.     if (a) {
  882.         twitter.accessToken(a, c)
  883.     } else {
  884.         if (!localStorage.firstRun) {
  885.             if (!localStorage.version) {
  886.                 chrome.tabs.create({
  887.                     url: "options/index.html"
  888.                 });
  889.                 localStorage.timeout = 10;
  890.                 localStorage.fontSize = 13;
  891.                 localStorage.version = twitter.version
  892.             } else {
  893.                 accounts = [{
  894.                     accessToken: localStorage.accessToken,
  895.                     accessTokenSecret: localStorage.accessTokenSecret,
  896.                     id: parseInt(localStorage.id),
  897.                     image: localStorage.image,
  898.                     name: localStorage.name,
  899.                     notifyDm: localStorage.notifyDm,
  900.                     notifyMention: localStorage.notifyMention,
  901.                     notifyTweet: localStorage.notifyTweet,
  902.                     screenName: localStorage.screenName
  903.                 }];
  904.                 for (var b in accounts[0]) {
  905.                     if (accounts[0][b] == "true") {
  906.                         accounts[0][b] = true
  907.                     }
  908.                     if (accounts[0][b] == "false") {
  909.                         accounts[0][b] = false
  910.                     }
  911.                 }
  912.                 localStorage.removeItem("screenName");
  913.                 localStorage.removeItem("id");
  914.                 localStorage.removeItem("accessToken");
  915.                 localStorage.removeItem("accessTokenSecret");
  916.                 localStorage.removeItem("image");
  917.                 localStorage.removeItem("name");
  918.                 localStorage.removeItem("notifyDm");
  919.                 localStorage.removeItem("notifyTweet");
  920.                 localStorage.removeItem("notifyMention");
  921.                 updateAccounts()
  922.             }
  923.             localStorage.firstRun = true
  924.         }
  925.         if (localStorage.version < twitter.version) {
  926.             localStorage.version = twitter.version;
  927.             webkitNotifications.createNotification("images/48.png", "Twitter Notifier Updated!", "Twitter Notifier has been updated to the latest and greatest version!").show()
  928.         }
  929.         twitter.setup()
  930.     }
  931. });