Last active
October 30, 2015 00:26
-
-
Save jbasinger/2189ac829bfdad67b547 to your computer and use it in GitHub Desktop.
Remote Control Firefox!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pageWorkers = require("sdk/page-worker"); | |
| pageWorker = pageWorkers.Page({ | |
| contentURL: self.data.url("worker.html"), | |
| contentScriptFile: [self.data.url('jquery-2.1.4.min.js'),self.data.url('worker.js')], | |
| contentScriptWhen: "ready" | |
| }); | |
| pageWorker.port.on('command', function(cmd){ | |
| switch(cmd){ | |
| case "toggle youtube": | |
| sendYoutubeCommand('toggle'); | |
| break; | |
| case "play youtube": | |
| sendYoutubeCommand('play'); | |
| break; | |
| case "pause youtube": | |
| sendYoutubeCommand('pause'); | |
| break; | |
| default: | |
| break; | |
| } | |
| }); | |
| function sendYoutubeCommand(cmd){ | |
| for(var i=0; i< youtubeTabs.length; i++){ | |
| var yt = youtubeTabs[i]; | |
| yt.worker.port.emit(cmd, 'cmd'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'json' | |
| set :queues, {} | |
| before do | |
| build_cors_headers | |
| end | |
| post '/queue/:qName' do |qName| | |
| halt 400, 'Use the "cmd" key' unless params["cmd"] | |
| q = get_queue(qName) | |
| q << params["cmd"]; | |
| end | |
| get '/queue/:qName' do |qName| | |
| q = get_queue(qName) | |
| if q.empty? then | |
| nil.to_json | |
| else | |
| q.pop.to_json | |
| end | |
| end | |
| options '*' do | |
| 200 | |
| end | |
| def build_cors_headers | |
| response.headers['Allow'] = 'HEAD,GET,PUT,POST,DELETE,OPTIONS' | |
| response.headers['Access-Control-Allow-Methods'] = 'HEAD,GET,PUT,POST,DELETE,OPTIONS' #['HEAD','GET','PUT','POST','DELETE','OPTIONS'] | |
| response.headers['Access-Control-Allow-Origin'] = '*' | |
| response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept' | |
| end | |
| def ensure_queue(qName) | |
| settings.queues[qName] = Queue.new unless settings.queues.has_key?(qName) | |
| end | |
| def get_queue(qName) | |
| ensure_queue(qName) | |
| settings.queues[qName] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var poll = setInterval(function(){ | |
| $.ajax({ | |
| url: "http://localhost:9292/queue/firefox", | |
| dataType: 'json' | |
| }).done(function(cmd){ | |
| if(cmd){ | |
| self.port.emit('command', cmd); | |
| } | |
| }).fail(function(e){ | |
| self.port.emit('workerError',e); | |
| }); | |
| }, 500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var self = require('sdk/self'); | |
| var tabs = require("sdk/tabs"); | |
| var URL = require("sdk/url").URL; | |
| var youtubeTabs = []; | |
| tabs.on('ready', function(tab){ | |
| if(URL(tab.url).host == 'www.youtube.com'){ | |
| var worker = tab.attach({ | |
| contentScriptFile: self.data.url('youtube.js') | |
| }); | |
| var obj = {'tab': tab, 'worker': worker}; | |
| youtubeTabs.push(obj); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| self.port.on('pause',function(sup){ | |
| var vid = document.getElementsByTagName("video")[0]; | |
| vid.pause(); | |
| }); | |
| self.port.on('play', function(){ | |
| var vid = document.getElementsByTagName("video")[0]; | |
| vid.play(); | |
| }); | |
| self.port.on('toggle', function(){Here we | |
| var vid = document.getElementsByTagName("video")[0]; | |
| if(vid.paused){ | |
| vid.play(); | |
| } else { | |
| vid.pause(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment