Skip to content

Commit

Permalink
Prepping for v0.6.2 release. Reformatted codebase using prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadBailey committed May 17, 2020
1 parent 204c106 commit 99a19a3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 89 deletions.
106 changes: 52 additions & 54 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function log(message, level) {
}
}

chrome.storage.sync.get(tc.settings, function(storage) {
chrome.storage.sync.get(tc.settings, function (storage) {
tc.settings.keyBindings = storage.keyBindings; // Array
if (storage.keyBindings.length == 0) {
// if first initialization of 0.5.3
Expand Down Expand Up @@ -124,7 +124,9 @@ chrome.storage.sync.get(tc.settings, function(storage) {
tc.settings.blacklist = String(storage.blacklist);

// ensure that there is a "display" binding (for upgrades from versions that had it as a separate binding)
if (tc.settings.keyBindings.filter(x => x.action == "display").length == 0) {
if (
tc.settings.keyBindings.filter((x) => x.action == "display").length == 0
) {
tc.settings.keyBindings.push({
action: "display",
key: Number(storage.displayKeyCode) || 86,
Expand All @@ -141,28 +143,28 @@ var forEach = Array.prototype.forEach;

function getKeyBindings(action, what = "value") {
try {
return tc.settings.keyBindings.find(item => item.action === action)[what];
return tc.settings.keyBindings.find((item) => item.action === action)[what];
} catch (e) {
return false;
}
}

function setKeyBindings(action, value) {
tc.settings.keyBindings.find(item => item.action === action)["value"] = value;
tc.settings.keyBindings.find((item) => item.action === action)[
"value"
] = value;
}

function defineVideoController() {
tc.videoController = function(target, parent) {
tc.videoController = function (target, parent) {
if (target.dataset["vscid"]) {
return target.vsc;
}

this.video = target;
this.parent = target.parentElement || parent;
this.document = target.ownerDocument;
this.id = Math.random()
.toString(36)
.substr(2, 9);
this.id = Math.random().toString(36).substr(2, 9);
storedSpeed = tc.settings.speeds[target.currentSrc];
if (!tc.settings.rememberSpeed) {
if (!storedSpeed) {
Expand All @@ -183,14 +185,11 @@ function defineVideoController() {

this.div = this.initializeControls();

var mediaEventAction = function(event) {
var mediaEventAction = function (event) {
storedSpeed = tc.settings.speeds[event.target.currentSrc];
if (!tc.settings.rememberSpeed) {
if (!storedSpeed) {
log(
"Overwriting stored speed to 1.0 (rememberSpeed not enabled)",
4
);
log("Overwriting stored speed to 1.0 (rememberSpeed not enabled)", 4);
storedSpeed = 1.0;
}
// resetSpeed isn't really a reset, it's a toggle
Expand All @@ -211,9 +210,9 @@ function defineVideoController() {
var controller = event.target.parentElement.querySelector(
".vsc-controller"
);

var video = controller.parentElement.querySelector("video");
setSpeed(controller, video, storedSpeed)
setSpeed(controller, video, storedSpeed);
};

target.addEventListener(
Expand All @@ -226,8 +225,8 @@ function defineVideoController() {
(this.handleSeek = mediaEventAction.bind(this))
);

var observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
var observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (
mutation.type === "attributes" &&
(mutation.attributeName === "src" ||
Expand All @@ -250,15 +249,15 @@ function defineVideoController() {
});
};

tc.videoController.prototype.remove = function() {
tc.videoController.prototype.remove = function () {
this.div.remove();
this.video.removeEventListener("play", this.handlePlay);
this.video.removeEventListener("seek", this.handleSeek);
delete this.video.dataset["vscid"];
delete this.video.vsc;
};

tc.videoController.prototype.initializeControls = function() {
tc.videoController.prototype.initializeControls = function () {
log("initializeControls Begin", 5);
var document = this.document;
var speed = this.video.playbackRate.toFixed(2),
Expand Down Expand Up @@ -301,17 +300,17 @@ function defineVideoController() {
shadow.innerHTML = shadowTemplate;
shadow.querySelector(".draggable").addEventListener(
"mousedown",
e => {
(e) => {
runAction(e.target.dataset["action"], document, false, e);
e.stopPropagation();
},
true
);

forEach.call(shadow.querySelectorAll("button"), function(button) {
forEach.call(shadow.querySelectorAll("button"), function (button) {
button.addEventListener(
"click",
e => {
(e) => {
runAction(
e.target.dataset["action"],
document,
Expand All @@ -326,10 +325,10 @@ function defineVideoController() {

shadow
.querySelector("#controller")
.addEventListener("click", e => e.stopPropagation(), false);
.addEventListener("click", (e) => e.stopPropagation(), false);
shadow
.querySelector("#controller")
.addEventListener("mousedown", e => e.stopPropagation(), false);
.addEventListener("mousedown", (e) => e.stopPropagation(), false);

this.speedIndicator = shadow.querySelector("span");
var fragment = document.createDocumentFragment();
Expand All @@ -346,10 +345,7 @@ function defineVideoController() {
break;
case location.hostname == "tv.apple.com":
// insert after parent for correct stacking context
this.parent
.getRootNode()
.querySelector(".scrim")
.prepend(fragment);
this.parent.getRootNode().querySelector(".scrim").prepend(fragment);

default:
// Note: when triggered via a MutationRecord, it's possible that the
Expand All @@ -368,7 +364,7 @@ function escapeStringRegExp(str) {

function isBlacklisted() {
blacklisted = false;
tc.settings.blacklist.split("\n").forEach(match => {
tc.settings.blacklist.split("\n").forEach((match) => {
match = match.replace(regStrip, "");
if (match.length == 0) {
return;
Expand Down Expand Up @@ -398,7 +394,7 @@ function refreshCoolDown() {
if (coolDown) {
clearTimeout(coolDown);
}
coolDown = setTimeout(function() {
coolDown = setTimeout(function () {
coolDown = false;
}, 1000);
log("End refreshCoolDown", 5);
Expand All @@ -407,7 +403,7 @@ function refreshCoolDown() {
function setupListener() {
document.body.addEventListener(
"ratechange",
function(event) {
function (event) {
if (coolDown) {
log("Speed event propagation blocked", 4);
event.stopImmediatePropagation();
Expand All @@ -425,7 +421,7 @@ function setupListener() {
log("Storing lastSpeed in settings for the rememberSpeed feature", 5);
tc.settings.lastSpeed = speed;
log("Syncing chrome settings for lastSpeed", 5);
chrome.storage.sync.set({ lastSpeed: speed }, function() {
chrome.storage.sync.set({ lastSpeed: speed }, function () {
log("Speed setting saved: " + speed, 5);
});
// show the controller for 1000ms if it's hidden.
Expand Down Expand Up @@ -482,7 +478,7 @@ function getShadow(parent) {
return result.flat(Infinity);
}
function getController(id) {
return getShadow(document.body).filter(x => {
return getShadow(document.body).filter((x) => {
return (
x.attributes["data-vscid"] &&
x.tagName == "DIV" &&
Expand Down Expand Up @@ -520,10 +516,10 @@ function initializeNow(document) {
if (inIframe()) docs.push(window.top.document);
} catch (e) {}

docs.forEach(function(doc) {
docs.forEach(function (doc) {
doc.addEventListener(
"keydown",
function(event) {
function (event) {
var keyCode = event.keyCode;
log("Processing keydown event: " + keyCode, 6);

Expand Down Expand Up @@ -552,12 +548,12 @@ function initializeNow(document) {

// Ignore keydown event if typing in a page without vsc
if (
!getShadow(document.body).filter(x => x.tagName == "vsc-controller")
!getShadow(document.body).filter((x) => x.tagName == "vsc-controller")
) {
return false;
}

var item = tc.settings.keyBindings.find(item => item.key === keyCode);
var item = tc.settings.keyBindings.find((item) => item.key === keyCode);
if (item) {
runAction(item.action, document, item.value);
if (item.force === "true") {
Expand Down Expand Up @@ -598,18 +594,18 @@ function initializeNow(document) {
}
}

var observer = new MutationObserver(function(mutations) {
var observer = new MutationObserver(function (mutations) {
// Process the DOM nodes lazily
requestIdleCallback(
_ => {
mutations.forEach(function(mutation) {
(_) => {
mutations.forEach(function (mutation) {
switch (mutation.type) {
case "childList":
forEach.call(mutation.addedNodes, function(node) {
forEach.call(mutation.addedNodes, function (node) {
if (typeof node === "function") return;
checkForVideo(node, node.parentNode || mutation.target, true);
});
forEach.call(mutation.removedNodes, function(node) {
forEach.call(mutation.removedNodes, function (node) {
if (typeof node === "function") return;
checkForVideo(node, node.parentNode || mutation.target, false);
});
Expand All @@ -620,9 +616,11 @@ function initializeNow(document) {
mutation.target.attributes["aria-hidden"].value == "false"
) {
var flattenedNodes = getShadow(document.body);
var node = flattenedNodes.filter(x => x.tagName == "VIDEO")[0];
var node = flattenedNodes.filter(
(x) => x.tagName == "VIDEO"
)[0];
if (node) {
var oldController = flattenedNodes.filter(x =>
var oldController = flattenedNodes.filter((x) =>
x.classList.contains("vsc-controller")
)[0];
if (oldController) {
Expand Down Expand Up @@ -654,12 +652,12 @@ function initializeNow(document) {
var mediaTags = document.querySelectorAll("video");
}

forEach.call(mediaTags, function(video) {
forEach.call(mediaTags, function (video) {
video.vsc = new tc.videoController(video);
});

var frameTags = document.getElementsByTagName("iframe");
forEach.call(frameTags, function(frame) {
forEach.call(frameTags, function (frame) {
// Ignore frames we don't have permission to access (different origin).
try {
var childDocument = frame.contentDocument;
Expand Down Expand Up @@ -687,11 +685,13 @@ function setSpeed(controller, video, speed) {
function runAction(action, document, value, e) {
log("runAction Begin", 5);
if (tc.settings.audioBoolean) {
var mediaTags = getShadow(document.body).filter(x => {
var mediaTags = getShadow(document.body).filter((x) => {
return x.tagName == "AUDIO" || x.tagName == "VIDEO";
});
} else {
var mediaTags = getShadow(document.body).filter(x => x.tagName == "VIDEO");
var mediaTags = getShadow(document.body).filter(
(x) => x.tagName == "VIDEO"
);
}

mediaTags.forEach = Array.prototype.forEach;
Expand All @@ -701,15 +701,13 @@ function runAction(action, document, value, e) {
var targetController = e.target.getRootNode().host;
}

mediaTags.forEach(function(v) {
mediaTags.forEach(function (v) {
var id = v.dataset["vscid"];
var controller = getController(id);
// if the controller isn't found, attempt to search the video element for the
// controller instead
if (!controller) {
controller = v.parentElement.querySelector(
".vsc-controller"
);
controller = v.parentElement.querySelector(".vsc-controller");
}

// Don't change video speed if the video has a different controller
Expand Down Expand Up @@ -855,7 +853,7 @@ function handleDrag(video, controller, e) {
parseInt(shadowController.style.top)
];

const startDragging = e => {
const startDragging = (e) => {
let style = shadowController.style;
let dx = e.clientX - initialMouseXY[0];
let dy = e.clientY - initialMouseXY[1];
Expand Down Expand Up @@ -886,7 +884,7 @@ function showController(controller) {
if (animation) clearTimeout(timer);

animation = true;
timer = setTimeout(function() {
timer = setTimeout(function () {
controller.classList.remove("vcs-show");
animation = false;
log("Hiding controller", 5);
Expand Down
13 changes: 7 additions & 6 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ <h3>Other</h3>

<h4>Extension controls not appearing?</h4>
<p>
This extension is only compatible with HTML5 audio and video. If you don't
see the controls showing up, chances are you are viewing a Flash content.
If you want to confirm, try right-clicking on the content and inspect the
menu: if it mentions flash, then that's the issue. That said, <b>most sites
will fallback to HTML5</b> if they detect that Flash is not available. You
can try manually disabling Flash plugin in the browser:
This extension is only compatible with HTML5 audio and video. If you
don't see the controls showing up, chances are you are viewing a Flash
content. If you want to confirm, try right-clicking on the content and
inspect the menu: if it mentions flash, then that's the issue. That
said, <b>most sites will fallback to HTML5</b> if they detect that Flash
is not available. You can try manually disabling Flash plugin in the
browser:
</p>

<ul>
Expand Down
Loading

0 comments on commit 99a19a3

Please sign in to comment.