| forums = { |
| get array () { |
| var arr = {3:{},4:{},5:{},6:{},7:{}}; |
| for (var i = 3; i <= 7; i++) { |
| arr[i].last_post_time = this[i].last_post_time; |
| arr[i].last_post_id = this[i].last_post_id; |
| arr[i].last_poster = this[i].last_poster; |
| arr[i].subject = this[i].subject; |
| arr[i].icon = this[i].icon; |
| } |
| return arr; |
| } |
| }; |
| for (var i = 3; i <= 7; i++) { |
| forums[i] = prototype = { |
| i: i, |
| get last_post_time () { |
| return localStorage['forum[' + this.i + '][last_post_time]'] ? |
| parseInt(localStorage['forum[' + this.i + '][last_post_time]']) : |
| 0; |
| }, |
| set last_post_time (v) { |
| localStorage['forum[' + this.i + '][last_post_time]'] = v; |
| }, |
| get last_post_id () { |
| return localStorage['forum[' + this.i + '][last_post_id]'] ? |
| parseInt(localStorage['forum[' + this.i + '][last_post_id]']) : |
| 0; |
| }, |
| set last_post_id (v) { |
| localStorage['forum[' + this.i + '][last_post_id]'] = v; |
| }, |
| get last_poster () { |
| return localStorage['forum[' + this.i + '][last_poster]']; |
| }, |
| set last_poster (v) { |
| localStorage['forum[' + this.i + '][last_poster]'] = v; |
| }, |
| get subject () { |
| return localStorage['forum[' + this.i + '][subject]']; |
| }, |
| set subject (v) { |
| localStorage['forum[' + this.i + '][subject]'] = v; |
| }, |
| get icon () { |
| return (localStorage['forum[' + this.i + '][icon]']) ? true : false; |
| }, |
| set icon (v) { |
| localStorage['forum[' + this.i + '][icon]'] = v; |
| } |
| } |
| } |
| var api = { |
| call: function (method, value, callback, error) { |
| var variable = ''; |
| if (typeof value === 'object') { |
| for (v in value) { |
| variable += '' + v + '=' + value[v] + '&'; |
| } |
| } |
| switch (method) { |
| case 'getMessage': |
| case 'getPrivateMessage': |
| case 'getPrivateMessages': |
| case 'getConfig': |
| case 'setMessage': |
| case 'getForums': |
| return $.getJSON('http://' + configs.host + '' + configs.patch_api + 'method=' + method + '&' + variable) |
| .success(callback) |
| .error(error); |
| break; |
| default: |
| return null; |
| } |
| } |
| }; |
| ports = { |
| popup: null, |
| notification: null, |
| contentscript: null |
| }; |
| chrome.extension.onConnect.addListener(function(port) { |
| if (port.sender.id === window.location.hostname) { |
| if (port.name === 'popup') |
| ports.popup = port; |
| else if (port.name === 'notification') |
| ports.notification = port; |
| else if (port.name === 'contentscript') |
| ports.contentscript = port; |
| else return; |
| port.onMessage.addListener(function (msg) { |
| // Проверка на соединение портов. Чет умнее не придумал. |
| if (typeof msg.connected !== 'undefined') |
| port.postMessage({connected: true}); |
| // Получение и изминение настроек см. method.settingInPort |
| else if (typeof msg.setting !== 'undefined') |
| port.postMessage({setting: method.settingInPort(msg.setting)}); |
| // Получение данных. А также их изменение (например отмерка как прочитаное) |
| else if (typeof msg.forum !== 'undefined') |
| port.postMessage({forum: method.forumInPort(msg.forum)}); |
| // Прямой доступ к апи. |
| else if (typeof msg.api !== 'undefined') |
| api.call(msg.method, msg.value, function (data) { |
| if (data.status === true) |
| port.postMessage({api: true, data: data.data}); |
| else |
| port.postMessage({api: true, error: data.data}); |
| }, |
| function (data) { |
| port.postMessage({api: true, error: data}); |
| }); |
| else if (typeof msg.reload !== 'undefined') |
| api.call('getForums', null, method.updateData); |
| }); |
| // Событие на разрыв соединения. |
| port.onDisconnect.addListener(function () { |
| ports[port.name] = null; |
| }); |
| } |
| }); |
| api.call('getConfig', null, settings.update); |
| method.startUpdateData(); |