Skip to content

Instantly share code, notes, and snippets.

@Tyyppi77
Last active July 5, 2019 11:15
Show Gist options
  • Save Tyyppi77/4264db000591e49c858d to your computer and use it in GitHub Desktop.
Save Tyyppi77/4264db000591e49c858d to your computer and use it in GitHub Desktop.
JavaScript userscript for navigating a generic JWPlayer video player, designed to work with DNA TV
// ==UserScript==
// @name DNA TV Shortcuts
// @description Shortcuts for DNA TV navigation.
// @version 1.0.1
// @include https://tv.dna.fi/*
// @run-at document-end
// @author Tyyppi_77
// ==/UserScript==
var MINUTE = 60;
var TWO_MINUTES = 2 * MINUTE;
var HALF_MINUTE = MINUTE / 2;
// Maps the constants to the seek amount for easier tweaking.
var LARGE_SEEK = TWO_MINUTES;
var MEDIUM_SEEK = MINUTE;
var SMALL_SEEK = HALF_MINUTE;
// The constant DNA TV player ID, used for getting the player element from the page.
var PLAYER_ID = "jwplayer1";
/*
Moves forward or backward (depending on the sign of "seconds") in the player.
*/
function seekSeconds(seconds) {
"use strict";
// Gets the player element.
var playerElement = jwplayer(PLAYER_ID);
// Seeks to the correct position.
playerElement.seek(playerElement.getPosition() + seconds);
}
// Maps the shortcuts. Uses a map for easy callback tweaking.
var KEY_SHORTCUTS = new Map();
// Adds the shortcuts to the map. The first element is the keyboard keyname capitalized, and then second
// element is a function that is assigned to the keyboard button.
KEY_SHORTCUTS.set("Q", function () {seekSeconds(-LARGE_SEEK); });
KEY_SHORTCUTS.set("W", function () {seekSeconds(-MEDIUM_SEEK); });
KEY_SHORTCUTS.set("E", function () {seekSeconds(-SMALL_SEEK); });
KEY_SHORTCUTS.set("R", function () {seekSeconds(SMALL_SEEK); });
KEY_SHORTCUTS.set("T", function () {seekSeconds(MEDIUM_SEEK); });
KEY_SHORTCUTS.set("Y", function () {seekSeconds(LARGE_SEEK); });
/*
Returns the key code from the event, decoupled to a separate function for better
cross-browser handling. Thanks IE.
*/
function getKeyCode(e) {
"use strict";
// This is for IE.
if (window.event) {
return e.keyCode;
} else { // And this is for other browsers.
if (e.which) {
return e.which;
}
}
return 0;
}
/*
Gets called by the document when a key is pressed.
*/
function onKeyPress(e) {
"use strict";
var keyString, keyCallback;
// Translates the key code into a string.
keyString = String.fromCharCode(getKeyCode(e));
// Executes the correct function.
keyCallback = KEY_SHORTCUTS.get(keyString);
if (keyCallback) {
keyCallback();
}
}
// Hooks up the callback.
window.onkeydown = onKeyPress;
window.onkeypress = onKeyPress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment