Class: TriggerHappy::InstallPageServer

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/trigger-happy/install_page_server.rb

Constant Summary collapse

BOOKMARKLET =
"  (function() {\n    var PageLoader = {};\n\n    PageLoader.init = function() {\n      while ( document.body.childNodes.length >= 1 ) {\n        document.body.removeChild( document.body.firstChild );       \n      }\n      document.documentElement.style.height = '100%';\n      document.body.style = {margin:0, height:'100%', overflow:'hidden'};\n\n      this.iframe = document.createElement('iframe');\n      this.iframe.setAttribute('src', document.location.href);\n      this.iframe.setAttribute('style', 'width:100%;height:100%;');\n      this.iframe.setAttribute('allowtransparency', 'true');\n      this.iframe.setAttribute('frameborder', '0');\n      this.iframe.setAttribute('sandbox', 'allow-same-origin allow-forms allow-scripts');\n      this.iframe.setAttribute('scrolling', 'auto');\n      document.body.appendChild(this.iframe);\n\n      this.msg = document.createElement('div');\n      this.msg.setAttribute('style', 'background-color:red;padding:10px;color:white;position:absolute;top:0;right:0;' );\n      document.body.appendChild(this.msg);\n\n      this.openSocket();\n    };\n\n    PageLoader.openSocket = function(text, permanent) {\n      var self = this,\n          ws = new WebSocket(\"ws://localhost:10101\");\n\n      ws.onmessage = function(evt) { \n        self.message(\"File Changed: \" +evt.data); \n        self.refresh();\n      };\n      ws.onclose = function() { \n        self.message(\"Server Not Running<br/>Launch with `trigger-happy`\", true);\n        window.setTimeout(function() {self.openSocket();},500);\n      };\n      ws.onopen = function() {\n        self.message(\"Connection to Server Opened\");\n      };\n      ws.onerror = function() {\n        self.message(\"An Error Has Occured\"); \n      };\n    };\n\n    PageLoader.message = function(text, permanent) {\n      var msgEl = this.msg;\n\n      msgEl.style.display = 'inherit';\n      msgEl.innerHTML = text;\n      if (!permanent) {window.setTimeout(function() {msgEl.style.display = 'none';},1500);}\n    };\n\n    PageLoader.refresh = function() {\n      this.iframe.contentWindow.location.reload(true);\n    };\n\n    PageLoader.init();\n  })();\n"
TEMPLATE =
ERB.new "  <!DOCTYPE HTML>\n  <html lang=\"en\">\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n    <title>Install Bookmarklet</title>\n    <meta name=\"author\" content=\"Alex Gibbons\">\n  </head>\n  <body>\n    <p>Drag this link: <a href=\"javascript:<%= URI.escape(BOOKMARKLET) %>\">Make TriggerHappy</a> up to your Bookmarks Toolbar</p>\n\n  </body>\n  </html>\n"

Instance Method Summary collapse

Instance Method Details

#do_GET(req, response) ⇒ Object



8
9
10
11
# File 'lib/trigger-happy/install_page_server.rb', line 8

def do_GET(req, response)
  response.body = TEMPLATE.result(binding)
  response['Content-Type'] = "text/html"
end