Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 131 lines (79 sloc) 3.11 kb
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



localStorage.host = localStorage.host or "localhost"
localStorage.port = localStorage.port or 32942

window.connection = null

loadSocketIO = ->
    clearTimeout loadSocketIO.timer
    console.log "trying to get io from #{ localStorage.host }:#{ localStorage.port }"
    if not window.io
        jQuery.getScript "http://#{ localStorage.host }:#{ localStorage.port }/socket.io/socket.io.js", ->
            window.connection = new Connection(localStorage.host, localStorage.port)
            initExtension()
            clearTimeout loadSocketIO.timer

        # TODO: Real error handling with real ajax calls
        loadSocketIO.timer = setTimeout loadSocketIO, 500
    else
        console.log "we aleready have io"



initExtension = ->

    chrome.contextMenus.create
        title: "Edit in external editor"
        contexts: ["editable",]
        onclick: ( onClickData, tab ) ->
            chrome.tabs.sendRequest tab.id, action: "edittextarea", onClickData: onClickData


    chrome.extension.onConnect.addListener (port) ->
        if port.name isnt "textareapipe"
            return

        port.onMessage.addListener (msg) ->
            connection.pageActions[msg.action](port, msg)







showTempNotification = (msg) ->

    notification = webkitNotifications.createNotification "icon.png", 'TextareaConnect', msg

    notification.show()
    setTimeout ->
        notification.cancel()
    , 5000



class Connection

    constructor: (@host, @port) ->
        @ports = {}
        @socket = null
        @pageActions =
            open: (port, msg) =>
                @ports[msg.uuid] = port
                msg.type = msg.type or "txt"
                @send msg

            delete: (port, msg) =>
                for uuid in msg.uuids
                    delete @ports[uuid]
                @send msg

        @initSocket()
        console.log "creating new socket #{@host}:#{@port}"

    initSocket: ->


        @socket?.disconnect()


        @socket = new io.Socket @host, port: @port

        @socket.on "message", (msg) =>
            obj = JSON.parse msg
            port = @ports[obj.uuid]
            port.postMessage obj


        @socket.on "connect", =>
            console.log "stopping connection poller"
            clearTimeout @reconnectTimer
            showTempNotification "Connected to TextareaServer at #{ @socket.transport.socket.URL }"

        @socket.on "disconnect", =>
            showTempNotification "Disconnected from TextareaServer at #{ @socket.transport.socket.URL }"
            @_reConnect()

        @socket.connect()

    send: (obj) ->
        if not @isConnected()
          alert "Sorry, not connected to TextareaServer"
        else
          @socket.send JSON.stringify obj

    isConnected: ->
        @socket?.connected

    _reConnect: ->

        # console.log "Trying to connect to #{ @socket.transport?.socket?.URL }"

        if not @isConnected()
            @socket.connect()

        clearTimeout @reconnectTimer

        # Retry
        @reconnectTimer = setTimeout =>
            @_reConnect()
        , 2000







loadSocketIO()
Something went wrong with that request. Please try again.