Guest User

Hacker news link navigator

a guest
Jul 22nd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Hacker news Link navigator
  3. // @namespace    https://news.ycombinator.com/
  4. // @version      0.1
  5. // @description  Read hacker news links and allow user to click on them with voice !
  6. // @author       Siddharth Chhaabra
  7. // @match        https://news.ycombinator.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.      function makeUniqueKey(anchorTagKeywords, items, anchorTag) {
  14.          for (var i = 0; i < anchorTagKeywords.length; i++) {
  15.              var key = anchorTagKeywords[i].toLowerCase();
  16.              if (key in items || key.includes("'") || key.includes('-')) {
  17.                  continue;
  18.              } else {
  19.                  var boldText = "<strong>" + anchorTagKeywords[i] + "</strong>";
  20.                  var anchorTagText = anchorTag.text;
  21.                  anchorTagText = anchorTagText.replace(anchorTagKeywords[i], boldText);
  22.                  anchorTag.innerHTML = anchorTagText;
  23.                  return key;
  24.              }
  25.          }
  26.      }
  27.      function getLinks() {
  28.          var items = {};
  29.          var links = document.getElementsByClassName('storylink');
  30.          for (var i = 0; i < links.length; i++) {
  31.              var anchorTag = links[i];
  32.              var anchorText = anchorTag.text;
  33.              var uniqueKey = makeUniqueKey(anchorText.split(' '), items, anchorTag);
  34.              items[uniqueKey] = anchorTag.href;
  35.          }
  36.          return items;
  37.      }
  38.     var linksDict = getLinks();
  39.     var linkNames = Object.keys(linksDict);
  40.     var grammar = '#JSGF V1.0; grammar link; public <link> = ' + linkNames.join(' | ') + ' ;';
  41.     var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
  42.     var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
  43.     var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
  44.     var recognition = new SpeechRecognition();
  45.     var speechRecognitionList = new SpeechGrammarList();
  46.     speechRecognitionList.addFromString(grammar, 1);
  47.     recognition.grammars = speechRecognitionList;
  48.     //recognition.continuous = false;
  49.     recognition.lang = 'en-US';
  50.     recognition.interimResults = false;
  51.     recognition.maxAlternatives = 1;
  52.     document.body.onclick = function() {
  53.         recognition.start();
  54.         console.log('Ready to receive a link naviagte command.');
  55.     };
  56.  
  57.     recognition.onresult = function(event) {
  58.         // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
  59.         // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
  60.         // It has a getter so it can be accessed like an array
  61.         // The [last] returns the SpeechRecognitionResult at the last position.
  62.         // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
  63.         // These also have getters so they can be accessed like arrays.
  64.         // The [0] returns the SpeechRecognitionAlternative at position 0.
  65.         // We then return the transcript property of the SpeechRecognitionAlternative object
  66.  
  67.         var last = event.results.length - 1;
  68.         var link = event.results[last][0].transcript;
  69.         link = link.toLowerCase();
  70.         alert(link);
  71.         console.log('Result received: ' + link);
  72.         if(linkNames.indexOf(link) > -1) {
  73.             window.location.href = linksDict[link];
  74.         }
  75.         console.log('Confidence: ' + event.results[0][0].confidence);
  76.     };
  77.  
  78.     recognition.onspeechend = function() {
  79.         recognition.stop();
  80.     };
  81.  
  82.     recognition.onnomatch = function(event) {
  83.         console.log("I didn't recognise that link.");
  84.     };
  85.  
  86.     recognition.onerror = function(event) {
  87.         console.log('Error occurred in recognition: ' + event.error);
  88.     };
  89.  
  90. })();
Add Comment
Please, Sign In to add comment