Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Hacker news Link navigator
- // @namespace https://news.ycombinator.com/
- // @version 0.1
- // @description Read hacker news links and allow user to click on them with voice !
- // @author Siddharth Chhaabra
- // @match https://news.ycombinator.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- function makeUniqueKey(anchorTagKeywords, items, anchorTag) {
- for (var i = 0; i < anchorTagKeywords.length; i++) {
- var key = anchorTagKeywords[i].toLowerCase();
- if (key in items || key.includes("'") || key.includes('-')) {
- continue;
- } else {
- var boldText = "<strong>" + anchorTagKeywords[i] + "</strong>";
- var anchorTagText = anchorTag.text;
- anchorTagText = anchorTagText.replace(anchorTagKeywords[i], boldText);
- anchorTag.innerHTML = anchorTagText;
- return key;
- }
- }
- }
- function getLinks() {
- var items = {};
- var links = document.getElementsByClassName('storylink');
- for (var i = 0; i < links.length; i++) {
- var anchorTag = links[i];
- var anchorText = anchorTag.text;
- var uniqueKey = makeUniqueKey(anchorText.split(' '), items, anchorTag);
- items[uniqueKey] = anchorTag.href;
- }
- return items;
- }
- var linksDict = getLinks();
- var linkNames = Object.keys(linksDict);
- var grammar = '#JSGF V1.0; grammar link; public <link> = ' + linkNames.join(' | ') + ' ;';
- var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
- var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
- var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
- var recognition = new SpeechRecognition();
- var speechRecognitionList = new SpeechGrammarList();
- speechRecognitionList.addFromString(grammar, 1);
- recognition.grammars = speechRecognitionList;
- //recognition.continuous = false;
- recognition.lang = 'en-US';
- recognition.interimResults = false;
- recognition.maxAlternatives = 1;
- document.body.onclick = function() {
- recognition.start();
- console.log('Ready to receive a link naviagte command.');
- };
- recognition.onresult = function(event) {
- // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
- // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
- // It has a getter so it can be accessed like an array
- // The [last] returns the SpeechRecognitionResult at the last position.
- // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
- // These also have getters so they can be accessed like arrays.
- // The [0] returns the SpeechRecognitionAlternative at position 0.
- // We then return the transcript property of the SpeechRecognitionAlternative object
- var last = event.results.length - 1;
- var link = event.results[last][0].transcript;
- link = link.toLowerCase();
- alert(link);
- console.log('Result received: ' + link);
- if(linkNames.indexOf(link) > -1) {
- window.location.href = linksDict[link];
- }
- console.log('Confidence: ' + event.results[0][0].confidence);
- };
- recognition.onspeechend = function() {
- recognition.stop();
- };
- recognition.onnomatch = function(event) {
- console.log("I didn't recognise that link.");
- };
- recognition.onerror = function(event) {
- console.log('Error occurred in recognition: ' + event.error);
- };
- })();
Add Comment
Please, Sign In to add comment