Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2017 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/6c71fd239b71f18a1b6d463055761211 to your computer and use it in GitHub Desktop.
Save anonymous/6c71fd239b71f18a1b6d463055761211 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name wykop voice commander/inteligentny wypok xD ALPHA/DEMO
// @namespace pomidor.com
// @version 0.1
// @description sterowanie wykopem glosem
// @author KwadratowyPomidor2
// @require https://code.jquery.com/jquery-1.12.4.min.js
// @match https://www.wykop.pl/*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function ($) {
//------------------------------------------------------------------
// actions, commands
//------------------------------------------------------------------
var actions = {
next: function() {
var $e = getFirstEntry();
if (!$e) {
return ;
}
$('html, body').animate({
scrollTop: $e.next().offset().top - 80
}, "fast");
},
prev: function() {
var $e = getFirstEntry();
if (!$e) {
return ;
}
$('html, body').animate({
scrollTop: $e.prev().offset().top - 80
}, "fast");
},
upvote: function() {
var $e = getFirstEntry();
if (!$e) {
return ;
}
var $a = $e.find(".author.ellipsis:first").find(".button.mikro.ajax");
$a.get(0).click();
}
};
var commands = {
"dalej": actions.next,
"wstecz": actions.prev,
"plus": actions.upvote,
"wyżej": function() {
$('html, body').animate({
scrollTop: $(window).scrollTop() - 400
}, "fast");
},
"niżej": function() {
$('html, body').animate({
scrollTop: $(window).scrollTop() + 400
}, "fast");
},
"24": function() {
location.href = 'https://www.wykop.pl/mikroblog/hot/ostatnie/24/';
},
"12": function() {
location.href = 'https://www.wykop.pl/mikroblog/hot/ostatnie/12/';
},
"6": function() {
location.href = 'https://www.wykop.pl/mikroblog/hot/ostatnie/6/';
},
"aktywne": function() {
location.href = 'https://www.wykop.pl/mikroblog/aktywne/';
},
"mirko": function() {
location.href = 'https://www.wykop.pl/mikroblog/wszystkie/';
},
"wykopalisko": function() {
location.href = 'https://www.wykop.pl/wykopalisko/';
},
"główna": function() {
location.href = 'https://www.wykop.pl/';
},
"wyłącz": function() {
deactivate();
}
};
// czasem glowna rozpoznaje jako gowno itd
commands["gówno"] = commands["główna"];
commands["aktywna"] = commands["aktywne"];
// aliasy
commands["wszystkie"] = commands["mirko"];
//------------------------------------------------------------------
// variables
//------------------------------------------------------------------
var recognizing = false;
var active = false;
//------------------------------------------------------------------
// recognition handler
//------------------------------------------------------------------
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + Object.keys(commands).join(' | ') + ' ;';
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'pl-PL';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.onresult = function(event) {
var last = event.results.length - 1;
var cmd = event.results[last][0].transcript.toLowerCase();
fetch(cmd, event);
};
recognition.onstart = function () {
recognizing = true;
};
recognition.onend = function () {
recognizing = false;
if (active) {
startRecognition();
}
};
recognition.onerror = function (event) {
recognizing = false;
if (active) {
startRecognition();
}
};
recognition.onspeechend = function() {
if (active) {
stopRecognition(); //?
startRecognition();
}
};
//------------------------------------------------------------------
// helpers functions
//------------------------------------------------------------------
var startRecognition = function() {
console.log('WVC.start');
if (!recognizing) {
try {
// to jest niedojebane bo recognition sie samo wylacza
// https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/continuous nie dziala:
// Note that continuous currently isn't implemented on Gecko, and you just single results with
// each recognition. For now, you can roughly emulate continuous results by just
// running SpeechRecognition.start() each time a result is detected, like this:
// recognition.onresult = function(event) {
// ...
// recognition.start();
//}
// i niestety to sypie exception ze
// recognition is started
recognition.start();
} catch (e) {
}
}
recognizing = true;
};
var stopRecognition = function() {
console.log('WVC.stop');
if (recognizing) {
try {
recognition.stop();
} catch (e) {
// to samo co wyżej
}
}
recognizing = false;
};
var dGet = function(key) {
return GM_getValue('wykopVoiceCmd_' + key);
};
var dSet = function(key, data) {
GM_setValue('wykopVoiceCmd_' + key, data);
};
var activate = function() {
active = 1;
dSet('active', 1);
$btn.find("a").css("background-color", "rgba(255, 0, 0, 0.3)");
startRecognition();
};
var deactivate = function() {
active = 0;
dSet('active', 0);
$btn.find("a").css("background-color", "rgba(255, 0, 0, 0.0)");
stopRecognition();
};
var fetch = function(cmd, event) {
//if (event.results[0][0].confidence < x) return ;
if (typeof commands[cmd] !== "undefined") {
console.log('WVC.fetch', cmd);
commands[cmd].call(this, event);
}else{
console.log('WVC.unknown_cmd', cmd);
}
};
var getFirstEntry = function() {
var ws = $(window).scrollTop();
var $entry = null;
$('#itemsStream > li.entry').each(function(){
var $t = $(this);
var s = $t.offset().top;
if (s >= ws) {
$entry = $t;
return false;
}
});
return $entry;
};
//------------------------------------------------------------------
// interface + init
//------------------------------------------------------------------
var $btn = $('<li class="notification"><a href="#"><i class="fa fa-bullhorn"></i></a></li>');
$('#nav .nav > ul:last-child .m-hide:first').after($btn);
$btn.find("a").click(function(e){
e.preventDefault();
if (active) {
deactivate();
}else{
activate();
}
});
if (dGet('active')) {
activate();
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment