Skip to content

Commit

Permalink
fixes #15 and #18
Browse files Browse the repository at this point in the history
  • Loading branch information
schomery committed Jul 22, 2017
1 parent 387222c commit f1cda04
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
11 changes: 4 additions & 7 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chrome.runtime.onMessage.addListener((request, sender, response) => {
});
}
else if (request.cmd === 'find-id') {
let req = new XMLHttpRequest();
const req = new XMLHttpRequest();
req.open('GET', request.url);
req.responseType = 'document';
req.onload = () => {
Expand All @@ -22,15 +22,12 @@ chrome.runtime.onMessage.addListener((request, sender, response) => {
req.send();
return true;
}
else if (request.cmd === 'is-mute') {
response(sender.tab.audible);
}
});

// FAQs & Feedback
chrome.storage.local.get('version', prefs => {
let version = chrome.runtime.getManifest().version;
let isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
const version = chrome.runtime.getManifest().version;
const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1;
if (isFirefox ? !prefs.version : prefs.version !== version) {
chrome.storage.local.set({version}, () => {
chrome.tabs.create({
Expand All @@ -41,6 +38,6 @@ chrome.storage.local.get('version', prefs => {
}
});
(function () {
let {name, version} = chrome.runtime.getManifest();
const {name, version} = chrome.runtime.getManifest();
chrome.runtime.setUninstallURL('http://add0n.com/feedback.html?name=' + name + '&version=' + version);
})();
14 changes: 12 additions & 2 deletions src/data/inject/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

.ihvyoutube:not([data-loaded=true]) {
background-image: url('moz-extension://__MSG_@@extension_id__/data/loader.gif'), url('chrome-extension://__MSG_@@extension_id__/data/loader.gif');
background-image: url('chrome-extension://__MSG_@@extension_id__/data/loader.gif');
}
.ihvyoutube[data-dark=true] {
box-shadow: 0 0 0 90000px rgba(0, 0, 0, 0.8);
Expand All @@ -28,6 +28,16 @@
.ihvyoutube[data-dark=true]:not([data-loaded=true]) {
}
.ihvyoutube[data-error=true] {
background-image: url('moz-extension://__MSG_@@extension_id__/data/error.svg'), url('chrome-extension://__MSG_@@extension_id__/data/error.svg');
background-image: url('chrome-extension://__MSG_@@extension_id__/data/error.svg');
background-size: 128px;
}

@-moz-document url-prefix() {
.ihvyoutube:not([data-loaded=true]) {
background-image: url('moz-extension://__MSG_@@extension_id__/data/loader.gif');
}
.ihvyoutube[data-error=true] {
background-image: url('moz-extension://__MSG_@@extension_id__/data/error.svg');
background-size: 128px;
}
}
67 changes: 38 additions & 29 deletions src/data/inject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ chrome.storage.onChanged.addListener(prefs => {
});
});

var smoothScroll = (function () {
var smoothScroll = (function() {
let timeLapsed = 0;
let id, sx, sy, dx, dy, callback;

let easingPattern = time => time < 0.5 ? 8 * time * time * time * time : 1 - 8 * (--time) * time * time * time;
const easingPattern = time => (time < 0.5) ?
(8 * time * time * time * time) :
(1 - 8 * (--time) * time * time * time);

function step () {
function step() {
timeLapsed += 16;
let percentage = timeLapsed / 400;
const percentage = timeLapsed / 400;
if (percentage > 1) {
window.scrollTo(sx + dx, sy + dy);
return callback();
Expand All @@ -42,7 +44,7 @@ var smoothScroll = (function () {
id = window.setTimeout(step, 16);
}

return function (x, y, c) {
return function(x, y, c) {
window.clearTimeout(id);
callback = c;
timeLapsed = 0;
Expand All @@ -59,13 +61,20 @@ var smoothScroll = (function () {

var youtube = {
play: (id, rect, shared) => {
// https://github.com/schomery/youtube-hover/issues/15
let time = (id.split(/[?&]t=/)[1] || '0').split('&')[0];
const tmp = /(?:(\d+)h)?(?:(\d+)m)?(\d+)s/.exec(time);
if (tmp && tmp.length && tmp[3]) {
time = Number(tmp[3]) + Number(tmp[2] || 0) * 60 + Number(tmp[1] || 0) * 60 * 60;
}
// cleaning id; https://github.com/schomery/youtube-hover/issues/12
id = id.split('&')[0].split('?')[0];
//
iframe = Object.assign(document.createElement('iframe'), {
width: config.width,
height: config.width * 180 / 320,
allowfullscreen: true,
sandbox: 'allow-scripts allow-same-origin',
// unload the gif loader when player is loaded
onload: () => {
window.setTimeout(() => {
Expand All @@ -76,22 +85,22 @@ var youtube = {
}
});

function play () {
function play() {
if (shared) {
chrome.runtime.sendMessage({
cmd: 'find-id',
url: 'https://www.youtube.com/shared?ci=' + id
}, id => {
if (id) {
iframe.setAttribute('src', `https://www.youtube.com/embed/${id}?autoplay=1&enablejsapi=1`);
iframe.setAttribute('src', `https://www.youtube.com/embed/${id}?autoplay=1&enablejsapi=1&start=${time}`);
}
else {
iframe.dataset.error = true;
}
});
}
else {
iframe.setAttribute('src', `https://www.youtube.com/embed/${id}?autoplay=1&enablejsapi=1`);
iframe.setAttribute('src', `https://www.youtube.com/embed/${id}?autoplay=1&enablejsapi=1&start=${time}`);
}
}

Expand All @@ -104,17 +113,17 @@ var youtube = {
play();
}
else {
let x1 = Math.max(0, rect.left + document.body.scrollLeft +
const x1 = Math.max(0, rect.left + document.body.scrollLeft +
document.documentElement.scrollLeft + config['relative-x']);
let y1 = Math.max(0, rect.top + rect.height + document.body.scrollTop +
const y1 = Math.max(0, rect.top + rect.height + document.body.scrollTop +
document.documentElement.scrollTop + config['relative-y']);
let x2 = x1 + config.width;
let y2 = y1 + config.width * 180 / 320;
let vw = Math.max(
const x2 = x1 + config.width;
const y2 = y1 + config.width * 180 / 320;
const vw = Math.max(
document.documentElement.scrollWidth,
document.body.scrollWidth
);
let vh = Math.max(
const vh = Math.max(
document.documentElement.scrollHeight,
document.body.scrollHeight
);
Expand All @@ -128,11 +137,11 @@ var youtube = {
top = vh - config.width * 180 / 320 - 10;
}
if (config.scroll) {
let x = Math.max(
const x = Math.max(
document.body.scrollLeft,
left + config.width - document.documentElement.clientWidth + 10
);
let y = Math.max(
const y = Math.max(
document.body.scrollTop,
top + config.width * 180 / 320 - document.documentElement.clientHeight + 10
);
Expand Down Expand Up @@ -165,11 +174,11 @@ document.addEventListener('mouseover', e => {
if (timer) {
timer = window.clearTimeout(timer);
}
let target = e.target;
const target = e.target;
if (target) {
let link = target.closest('a');
const link = target.closest('a');
if (link) {
let href = link.href;
const href = link.href;
if (!href || iframe) {
return;
}
Expand All @@ -182,26 +191,26 @@ document.addEventListener('mouseover', e => {
) {
let id;
if (href.indexOf('youtube.com/watch') !== -1) {
id = href.match(/v\=([^\&]+)/);
id = href.match(/v=(.+)/);
}
else if (href.indexOf('//youtu.be/') !== -1) {
id = href.match(/\.be\/([^\&]+)/);
id = href.match(/\.be\/(.+)/);
}
else if (href.indexOf('youtube.com/attribution_link') !== -1) {
id = decodeURIComponent(href).match(/v\=([^\&]+)/);
id = decodeURIComponent(href).match(/v=(.+)/);
}
else if (href.indexOf('youtube.com/shared') !== -1) {
shared = true;
id = href.match(/ci\=([^\&]+)/);
id = href.match(/ci=(.+)/);
}

if (id && id.length) {
timer = window.setTimeout((link) => {
let rect = link.getBoundingClientRect();
timer = window.setTimeout(link => {
const rect = link.getBoundingClientRect();
youtube.play(id[1], rect, shared);
if (config.strike) {
[...document.querySelectorAll(`a[href="${href}"]`), link].
forEach(l => l.style['text-decoration'] = 'line-through');
[...document.querySelectorAll(`a[href="${href}"]`), link]
.forEach(l => l.style['text-decoration'] = 'line-through');
}
if (config.history) {
chrome.runtime.sendMessage({
Expand All @@ -215,15 +224,15 @@ document.addEventListener('mouseover', e => {
}
}
});
document.addEventListener('click', (e) => {
document.addEventListener('click', e => {
if (iframe && e.target.closest('.ihvyoutube') === null) {
[...document.querySelectorAll('.ihvyoutube')].forEach(f => f.parentNode.removeChild(f));
iframe = null;
e.preventDefault();
}
});
// keydown
document.addEventListener('keydown', (e) => {
document.addEventListener('keydown', e => {
if (iframe && e.code === 'Escape') {
document.body.dispatchEvent(new Event('click', {bubbles: true}));
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"short_name": "ihvyoutube",
"description": "View YouTube while hovering mouse over a YouTube link on any website",
"author": "Jeremy Schomery",
"version": "0.1.7",
"version": "0.1.8",
"manifest_version": 2,
"permissions": [
"storage",
"tabs",
"history",
"*://*/*"
"*://www.youtube.com/*"
],
"background": {
"persistent": false,
Expand Down

0 comments on commit f1cda04

Please sign in to comment.