|
| 1 | +var $ = {}; |
| 2 | +$.x = function() { |
| 3 | + if (typeof XMLHttpRequest !== "undefined") { |
| 4 | + return new XMLHttpRequest(); |
| 5 | + } |
| 6 | + var versions = [ |
| 7 | + "MSXML2.XmlHttp.6.0", |
| 8 | + "MSXML2.XmlHttp.5.0", |
| 9 | + "MSXML2.XmlHttp.4.0", |
| 10 | + "MSXML2.XmlHttp.3.0", |
| 11 | + "MSXML2.XmlHttp.2.0", |
| 12 | + "Microsoft.XmlHttp" |
| 13 | + ]; |
| 14 | + |
| 15 | + var xhr; |
| 16 | + for (var i = 0; i < versions.length; i++) { |
| 17 | + try { |
| 18 | + xhr = new ActiveXObject(versions[i]); |
| 19 | + break; |
| 20 | + } catch (e) {} |
| 21 | + } |
| 22 | + return xhr; |
| 23 | +}; |
| 24 | + |
| 25 | +$.send = function(url, callback, method, data, async) { |
| 26 | + if (async === undefined) { |
| 27 | + async = true; |
| 28 | + } |
| 29 | + var x = $.x(); |
| 30 | + x.open(method, url, async); |
| 31 | + x.onreadystatechange = function() { |
| 32 | + if (x.readyState == 4) { |
| 33 | + if (typeof callback == "function") { |
| 34 | + callback(x.responseText); |
| 35 | + } |
| 36 | + } |
| 37 | + }; |
| 38 | + if (method == "POST") { |
| 39 | + x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
| 40 | + } |
| 41 | + x.send(data); |
| 42 | +}; |
| 43 | + |
| 44 | +$.get = function(url, data, callback, async) { |
| 45 | + var query = []; |
| 46 | + for (var key in data) { |
| 47 | + query.push( |
| 48 | + encodeURIComponent(key) + "=" + encodeURIComponent(data[key]) |
| 49 | + ); |
| 50 | + } |
| 51 | + $.send( |
| 52 | + url + (query.length ? "?" + query.join("&") : ""), |
| 53 | + callback, |
| 54 | + "GET", |
| 55 | + null, |
| 56 | + async |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +$.post = function(url, data, callback, async) { |
| 61 | + var query = []; |
| 62 | + for (var key in data) { |
| 63 | + query.push( |
| 64 | + encodeURIComponent(key) + "=" + encodeURIComponent(data[key]) |
| 65 | + ); |
| 66 | + } |
| 67 | + $.send(url, callback, "POST", query.join("&"), async); |
| 68 | +}; |
| 69 | + |
| 70 | +var NEW_NOTIFICATION = false; |
| 71 | +/** |
| 72 | + * @param Update noticcation count of the extension |
| 73 | + * @return {[type]} |
| 74 | + */ |
| 75 | +var updateNotification = function(count) { |
| 76 | + count = count > 99 ? "99+" : count; |
| 77 | + chrome.browserAction.setBadgeText({ text: count.toString() }); |
| 78 | +}; |
| 79 | +/** |
| 80 | + * Retrieves site meta info |
| 81 | + * @param {object} |
| 82 | + * @param {Function} Callback function |
| 83 | + */ |
| 84 | +var retrieveSiteMeta = function(passed_message, callback) { |
| 85 | + chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { |
| 86 | + if (tabs[0] && tabs[0].id) { |
| 87 | + chrome.tabs.sendMessage(tabs[0].id, passed_message, function( |
| 88 | + response |
| 89 | + ) { |
| 90 | + callback(response); |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | +}; |
| 95 | + |
| 96 | +var countStore = 0; |
| 97 | +var countData = { rows: [], pages: 0 }; |
| 98 | +var endpoint = "http://localhost:8000"; |
| 99 | + |
| 100 | +function checkStorage() { |
| 101 | + if (typeof localStorage.richNotification === "undefined") { |
| 102 | + localStorage.richNotification = 1; |
| 103 | + } |
| 104 | + if (typeof localStorage.sound === "undefined") { |
| 105 | + localStorage.sound = 1; |
| 106 | + } |
| 107 | + localStorage.theme = "dark"; |
| 108 | +} |
| 109 | +/** |
| 110 | + * This function is being called by the popup to get the notication count |
| 111 | + * for various tabs |
| 112 | + * @param {Function} Callback function |
| 113 | + */ |
| 114 | +var getNotifications = function(callback) { |
| 115 | + callback(countData); |
| 116 | +}; |
| 117 | + |
| 118 | +var getTitle = function(linkCount, commentCount, word) { |
| 119 | + const getVerb = (count, word) => { |
| 120 | + return count > 1 ? word + "s" : word; |
| 121 | + }; |
| 122 | + var msg = "You have got "; |
| 123 | + if (linkCount > 0) { |
| 124 | + msg += linkCount + " " + getVerb(linkCount, "link"); |
| 125 | + } |
| 126 | + |
| 127 | + if (commentCount > 0) { |
| 128 | + if (linkCount > 0) { |
| 129 | + msg += " and "; |
| 130 | + } |
| 131 | + msg += commentCount + " " + getVerb(commentCount, "comment"); |
| 132 | + } |
| 133 | + return msg; |
| 134 | +}; |
| 135 | +//Start polling |
| 136 | +setInterval(function() { |
| 137 | + checkStorage(); |
| 138 | + if (chrome && chrome.storage && chrome.storage.sync) { |
| 139 | + chrome.storage.sync.get("userid", function(items) { |
| 140 | + var userid = items.userid; |
| 141 | + var group = localStorage.getItem("defaultGroup"); |
| 142 | + if (userid && group) { |
| 143 | + $.get( |
| 144 | + endpoint, |
| 145 | + { |
| 146 | + group: group, |
| 147 | + action: "getActivities", |
| 148 | + bg: 1, |
| 149 | + chrome_id: userid |
| 150 | + }, |
| 151 | + function(data) { |
| 152 | + nData = data; |
| 153 | + //data contains wallCount and updateCount |
| 154 | + var totalCount = data.rows.length; |
| 155 | + var views = chrome.extension.getViews({ |
| 156 | + type: "popup" |
| 157 | + }); |
| 158 | + |
| 159 | + if (data.rows.length > 0 && views.length === 0) { |
| 160 | + chrome.browserAction.setBadgeText({ |
| 161 | + text: totalCount.toString() |
| 162 | + }); |
| 163 | + |
| 164 | + var sound = localStorage.sound; |
| 165 | + var richNotification = |
| 166 | + localStorage.richNotification; |
| 167 | + |
| 168 | + if (sound !== null && sound == "1") { |
| 169 | + var yourSound = new Audio( |
| 170 | + "../public/sound/noti.mp3" |
| 171 | + ); |
| 172 | + yourSound.play(); |
| 173 | + } |
| 174 | + |
| 175 | + if ( |
| 176 | + richNotification !== null && |
| 177 | + richNotification == "1" |
| 178 | + ) { |
| 179 | + var itemList = []; |
| 180 | + var linkCount = 0; |
| 181 | + var commentCount = 0; |
| 182 | + |
| 183 | + data.rows.forEach(activity => { |
| 184 | + var type = "link"; |
| 185 | + if (activity.type == "link") { |
| 186 | + linkCount++; |
| 187 | + } else if (activity.type == "comment") { |
| 188 | + commentCount++; |
| 189 | + } |
| 190 | + var regex_clean = /<\/?[^>]+(>|$)/g; |
| 191 | + itemList.push({ |
| 192 | + title: activity.template.replace( |
| 193 | + regex_clean, |
| 194 | + "" |
| 195 | + ), |
| 196 | + message: activity.type |
| 197 | + }); |
| 198 | + }); |
| 199 | + var title = getTitle(linkCount, commentCount); |
| 200 | + |
| 201 | + var options = { |
| 202 | + type: "list", |
| 203 | + message: "New Links posted", |
| 204 | + title: title, |
| 205 | + iconUrl: chrome.runtime.getURL( |
| 206 | + "assets/notification.png" |
| 207 | + ), |
| 208 | + items: itemList |
| 209 | + }; |
| 210 | + |
| 211 | + if (chrome.notifications) { |
| 212 | + chrome.notifications.create( |
| 213 | + new Date().toString(), |
| 214 | + options |
| 215 | + ); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + countData = data; |
| 220 | + localStorage.lastUpdateId = parseInt(data.lastUpdateId); |
| 221 | + } |
| 222 | + ); |
| 223 | + } |
| 224 | + }); |
| 225 | + } |
| 226 | +}, 15000); |
| 227 | + |
| 228 | +var sendClickedStat = function(data) { |
| 229 | + $.post(url, data); |
| 230 | +}; |
| 231 | + |
| 232 | +window.nData = {}; |
| 233 | +chrome.notifications.onClicked.addListener(t => { |
| 234 | + window.open(nData.rows[0].url); |
| 235 | +}); |
| 236 | + |
| 237 | +//update the version |
| 238 | +var updateVersion = function() { |
| 239 | + var manifest = chrome.runtime.getManifest(); |
| 240 | + var version = manifest.version; |
| 241 | + |
| 242 | + /* Sometimes due to some x bug, user gets loggedout. This will make them login automatically. */ |
| 243 | + chrome.storage.sync.get("userid", function(response) { |
| 244 | + if (response.userid) { |
| 245 | + //user has logged in once. check if the user purposely logged-out |
| 246 | + if (localStorage.getItem("loggedOut") != "true") { |
| 247 | + //login the user |
| 248 | + $.get( |
| 249 | + endpoint, |
| 250 | + { chrome_id: response.userid, action: "fetchUserInfo" }, |
| 251 | + function(result) { |
| 252 | + if (typeof localStorage.chrome_id == "undefined") { |
| 253 | + localStorage.nickname = result.data.nickname; |
| 254 | + localStorage.loggedIn = true; |
| 255 | + localStorage.chrome_id = response.userid; |
| 256 | + localStorage.richNotification = 1; |
| 257 | + localStorage.sound = 1; |
| 258 | + localStorage.theme = "dark"; |
| 259 | + localStorage.uid = result.data.id; |
| 260 | + localStorage.defaultGroup = 1; |
| 261 | + localStorage.defaultGroupName = "Global"; |
| 262 | + } |
| 263 | + } |
| 264 | + ); |
| 265 | + } |
| 266 | + } |
| 267 | + }); |
| 268 | + |
| 269 | + var chrome_id = localStorage.getItem("chrome_id"); |
| 270 | + if (chrome_id !== null) { |
| 271 | + $.post(endpoint, { |
| 272 | + version: version, |
| 273 | + chrome_id: chrome_id, |
| 274 | + action: "updateUserVersion" |
| 275 | + }); |
| 276 | + } |
| 277 | +}; |
| 278 | +/** |
| 279 | + * If the extension is installed or updated, update the version |
| 280 | + * in the server |
| 281 | + */ |
| 282 | +chrome.runtime.onInstalled.addListener(function(details) { |
| 283 | + updateVersion(); |
| 284 | +}); |
| 285 | + |
| 286 | +/** |
| 287 | + * Check for updates every 2 hours |
| 288 | + */ |
| 289 | +setInterval(function() { |
| 290 | + if ( |
| 291 | + chrome.runtime && |
| 292 | + typeof chrome.runtime.requestUpdateCheck === "function" |
| 293 | + ) { |
| 294 | + chrome.runtime.requestUpdateCheck(function() {}); |
| 295 | + } |
| 296 | +}, 1000 * 3600 * 2); |
0 commit comments