Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 9dcc958ce9981ca9f78d01e1e3db244be2588ecd Philipp Schaffrath committed Dec 30, 2012
@@ -0,0 +1,6 @@
+*.pyc
+*.cache
+*.sublime-project
+.DS_Store
+*.swp
+*.tmp
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,126 @@
+#localhost:1025
+#192.169.0.2:1025
+
+class tabFactory
+ constructor: () ->
+ @tabs = []
+ return
+
+ # adds a new tab instance to the tabs array
+ addTab: (tabId) ->
+ tab = new tabHandler(tabId, @)
+ @tabs[tabId] = tab
+ return tab
+
+ # sets a tab to undefinde after it called the disconnect method of it
+ removeTab: (tabId) ->
+ tab = @getTab tabId
+ if tab isnt false
+ tab.disconnect()
+ @tabs[tabId] = undefined
+ return
+
+ # returns a tab if available, else false
+ getTab: (tabId) ->
+ if @tabs[tabId] is undefined
+ return false
+ else
+ return @tabs[tabId]
+
+ # console.logs all tabs
+ logTabs: ->
+ console.log @tabs
+ return
+
+
+
+class tabHandler
+ constructor: (@tabId, @factory, @isClosed = false) ->
+
+ connect: (host, port) ->
+ @host = if host is null or host is "" then "localhost" else host
+ @port = if port is null or port is "" or port > 65535 or port < 1 then 1025 else port
+
+ @socket = new WebSocket("ws://#{@host}:#{@port}")
+ @socket.tabHandler = @
+
+ @socket.onopen = (data) ->
+ @tabHandler.setTabIcon()
+ return
+
+ @socket.onclose = (data) ->
+ if not @tabHandler.getIsClosed() then @tabHandler.setTabIcon()
+ @tabHandler.factory.removeTab @tabHandler.tabId
+ return
+
+ @socket.onmessage = (data) ->
+ if data.data is "refresh"
+ chrome.tabs.update @tabHandler.tabId, {selected: true}
+ chrome.tabs.reload @tabHandler.tabId
+ return
+
+ @socket.onerror = (data) ->
+ return
+
+ disconnect: () ->
+ @socket.close()
+
+ refreshTab: () ->
+
+ setTabIcon: () ->
+ icon = "icon.png"
+ # 0: connection has not yet been established
+ # 1: connection is established and communication is possible
+ # 2: connection is going through the closing handshake
+ # 3: connection has been closed or could not be opened
+ icon = "icon_active.png" if @socket.readyState is 1
+ chrome.tabs.get @tabId, (tab) ->
+ if tab then chrome.browserAction.setIcon {path:"img/#{icon}", tabId: tab.id}
+
+ getHost: -> return @host
+ getPort: -> return @port
+ getTab: -> return @tab
+ getSocket: -> return @socket
+ getIsClosed: -> return @isClosed
+
+ setIsClosed: (closed) ->
+ @isClosed = closed
+
+ getInformation: () ->
+ return {host: @host, port: @port}
+
+
+class socketStates
+ constructor: ->
+ @NONE = 0
+ @CONNECTING = 1
+ @CONNECTED = 2
+ @ERROR = 3
+
+
+factory = new tabFactory()
+
+connect = (host, port) ->
+ chrome.tabs.query {active: yes, currentWindow: yes}, (tabs) ->
+ tab = factory.addTab tabs[0].id
+ tab.connect(host, port)
+
+disconnect = ->
+ chrome.tabs.query {active: yes, currentWindow: yes}, (tabs) ->
+ tab = factory.getTab tabs[0].id
+ if tab isnt false then factory.removeTab tabs[0].id
+
+chrome.tabs.onUpdated.addListener (tabId, changeInfo, tab) ->
+ tab = factory.getTab tabId
+ if tab isnt false then tab.setTabIcon()
+
+chrome.tabs.onRemoved.addListener (tabId, removeInfo) ->
+ factory.removeTab(tabId)
+
+chrome.extension.onMessage.addListener (request, sender, sendResponse) ->
+ chrome.tabs.query {active: yes, currentWindow: yes}, (tabs) ->
+ tab = factory.getTab tabs[0].id
+ if request.command is "getInformation"
+ if tab isnt false
+ sendResponse tab.getInformation()
+ return true
@@ -0,0 +1,197 @@
+// Generated by CoffeeScript 1.4.0
+var connect, disconnect, factory, socketStates, tabFactory, tabHandler;
+
+tabFactory = (function() {
+
+ function tabFactory() {
+ this.tabs = [];
+ return;
+ }
+
+ tabFactory.prototype.addTab = function(tabId) {
+ var tab;
+ tab = new tabHandler(tabId, this);
+ this.tabs[tabId] = tab;
+ return tab;
+ };
+
+ tabFactory.prototype.removeTab = function(tabId) {
+ var tab;
+ tab = this.getTab(tabId);
+ if (tab !== false) {
+ tab.disconnect();
+ this.tabs[tabId] = void 0;
+ }
+ };
+
+ tabFactory.prototype.getTab = function(tabId) {
+ if (this.tabs[tabId] === void 0) {
+ return false;
+ } else {
+ return this.tabs[tabId];
+ }
+ };
+
+ tabFactory.prototype.logTabs = function() {
+ console.log(this.tabs);
+ };
+
+ return tabFactory;
+
+})();
+
+tabHandler = (function() {
+
+ function tabHandler(tabId, factory, isClosed) {
+ this.tabId = tabId;
+ this.factory = factory;
+ this.isClosed = isClosed != null ? isClosed : false;
+ }
+
+ tabHandler.prototype.connect = function(host, port) {
+ this.host = host === null || host === "" ? "localhost" : host;
+ this.port = port === null || port === "" || port > 65535 || port < 1 ? 1025 : port;
+ this.socket = new WebSocket("ws://" + this.host + ":" + this.port);
+ this.socket.tabHandler = this;
+ this.socket.onopen = function(data) {
+ this.tabHandler.setTabIcon();
+ };
+ this.socket.onclose = function(data) {
+ if (!this.tabHandler.getIsClosed()) {
+ this.tabHandler.setTabIcon();
+ }
+ this.tabHandler.factory.removeTab(this.tabHandler.tabId);
+ };
+ this.socket.onmessage = function(data) {
+ if (data.data === "refresh") {
+ chrome.tabs.update(this.tabHandler.tabId, {
+ selected: true
+ });
+ chrome.tabs.reload(this.tabHandler.tabId);
+ }
+ };
+ return this.socket.onerror = function(data) {};
+ };
+
+ tabHandler.prototype.disconnect = function() {
+ return this.socket.close();
+ };
+
+ tabHandler.prototype.refreshTab = function() {};
+
+ tabHandler.prototype.setTabIcon = function() {
+ var icon;
+ icon = "icon.png";
+ if (this.socket.readyState === 1) {
+ icon = "icon_active.png";
+ }
+ return chrome.tabs.get(this.tabId, function(tab) {
+ if (tab) {
+ return chrome.browserAction.setIcon({
+ path: "img/" + icon,
+ tabId: tab.id
+ });
+ }
+ });
+ };
+
+ tabHandler.prototype.getHost = function() {
+ return this.host;
+ };
+
+ tabHandler.prototype.getPort = function() {
+ return this.port;
+ };
+
+ tabHandler.prototype.getTab = function() {
+ return this.tab;
+ };
+
+ tabHandler.prototype.getSocket = function() {
+ return this.socket;
+ };
+
+ tabHandler.prototype.getIsClosed = function() {
+ return this.isClosed;
+ };
+
+ tabHandler.prototype.setIsClosed = function(closed) {
+ return this.isClosed = closed;
+ };
+
+ tabHandler.prototype.getInformation = function() {
+ return {
+ host: this.host,
+ port: this.port
+ };
+ };
+
+ return tabHandler;
+
+})();
+
+socketStates = (function() {
+
+ function socketStates() {
+ this.NONE = 0;
+ this.CONNECTING = 1;
+ this.CONNECTED = 2;
+ this.ERROR = 3;
+ }
+
+ return socketStates;
+
+})();
+
+factory = new tabFactory();
+
+connect = function(host, port) {
+ return chrome.tabs.query({
+ active: true,
+ currentWindow: true
+ }, function(tabs) {
+ var tab;
+ tab = factory.addTab(tabs[0].id);
+ return tab.connect(host, port);
+ });
+};
+
+disconnect = function() {
+ return chrome.tabs.query({
+ active: true,
+ currentWindow: true
+ }, function(tabs) {
+ var tab;
+ tab = factory.getTab(tabs[0].id);
+ if (tab !== false) {
+ return factory.removeTab(tabs[0].id);
+ }
+ });
+};
+
+chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
+ tab = factory.getTab(tabId);
+ if (tab !== false) {
+ return tab.setTabIcon();
+ }
+});
+
+chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
+ return factory.removeTab(tabId);
+});
+
+chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
+ chrome.tabs.query({
+ active: true,
+ currentWindow: true
+ }, function(tabs) {
+ var tab;
+ tab = factory.getTab(tabs[0].id);
+ if (request.command === "getInformation") {
+ if (tab !== false) {
+ return sendResponse(tab.getInformation());
+ }
+ }
+ });
+ return true;
+});
Oops, something went wrong.
@@ -0,0 +1,25 @@
+$(document).ready ->
+ background = chrome.extension.getBackgroundPage()
+ hostField = $('form#socketform #host')
+ portField = $('form#socketform #port')
+ connectButton = $('form#socketform #connect')
+ disconnectButton = $('form#socketform #disconnect')
+
+ $('#connect').on "click", (e) ->
+ e.preventDefault()
+ return if $(e.target).hasClass("disabled")
+ background.connect hostField.val(), portField.val()
+ window.close()
+
+ $('#disconnect').on "click", (e) ->
+ e.preventDefault()
+ return if $(e.target).hasClass("disabled")
+ background.disconnect()
+ window.close()
+
+
+ chrome.extension.sendMessage {command: "getInformation"}, (response) ->
+ hostField.val response.host
+ portField.val response.port
+ connectButton.addClass 'disabled'
+ disconnectButton.removeClass 'disabled'
@@ -0,0 +1,34 @@
+// Generated by CoffeeScript 1.4.0
+
+$(document).ready(function() {
+ var background, connectButton, disconnectButton, hostField, portField;
+ background = chrome.extension.getBackgroundPage();
+ hostField = $('form#socketform #host');
+ portField = $('form#socketform #port');
+ connectButton = $('form#socketform #connect');
+ disconnectButton = $('form#socketform #disconnect');
+ $('#connect').on("click", function(e) {
+ e.preventDefault();
+ if ($(e.target).hasClass("disabled")) {
+ return;
+ }
+ background.connect(hostField.val(), portField.val());
+ return window.close();
+ });
+ $('#disconnect').on("click", function(e) {
+ e.preventDefault();
+ if ($(e.target).hasClass("disabled")) {
+ return;
+ }
+ background.disconnect();
+ return window.close();
+ });
+ return chrome.extension.sendMessage({
+ command: "getInformation"
+ }, function(response) {
+ hostField.val(response.host);
+ portField.val(response.port);
+ connectButton.addClass('disabled');
+ return disconnectButton.removeClass('disabled');
+ });
+});
Oops, something went wrong.

0 comments on commit 9dcc958

Please sign in to comment.