/ Pedal_For_Youtube / youtube_stop_key.user.js
youtube_stop_key.user.js
 1  // ==UserScript==
 2  // @name          Youtube Pause Key
 3  // @version       1
 4  // @namespace     http://unpythonic.net.com/youtube-pause-key
 5  // @include       http://youtube.com/*
 6  // @include       https://youtube.com/*
 7  // @include       http://www.youtube.com/*
 8  // @include       https://www.youtube.com/*
 9  // @grant none
10  // ==/UserScript==
11  
12  (function () {
13      var inject = function() {
14          var is_press_key = function (e, key) {
15              document.last_target = e.target;
16              document.last_event = e;
17              var keyCode = e.keyCode ? e.keyCode : e.which;
18              return (! (e.altKey || e.ctrlKey || e.metaKey)
19                      && keyCode == key);
20          }
21  
22          var gr_in_bg_event = function(e) {
23              var pause_key = 112; // 'p' key
24              var videos = e.target.getElementsByTagName('video');
25              if(videos.length == 0) { return true; }
26              if (is_press_key(e, pause_key)) {
27                  videos[0].pause();
28                  e.stopPropagation();
29                  e.preventDefault();
30                  return;
31              }
32              return true;
33          }
34  
35          document.addEventListener("keypress", gr_in_bg_event, true);
36      }
37  
38      var newel = document.createElement("script");
39      var newcontent = document.createTextNode("(" + inject + ")()")
40      newel.appendChild(newcontent);
41      document.body.appendChild(newel);
42  })()