Skip to content

Commit

Permalink
simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
x94fujo6rpg committed Dec 4, 2020
1 parent 47c4260 commit ace18f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 82 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,10 @@ remove bilibili article copy protection
normalize url in video description
remove miniplayer (still activated when you watch in playlist)

- v0.02 fixed

## [[prts redirector]](https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/prts_redirector.user.js)
Arknights Wiki PRTS
auto redirect & replace all link to desktop version

- v0.2 replace all link too

## [[sharer.pw auto click]](https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/sharer-pw_auto_click.user.js)
auto click and redirect to download link immediately

Expand Down
101 changes: 23 additions & 78 deletions ytb_url_normalizer.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/ytb_url_normalizer.user.js
// @downloadURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/ytb_url_normalizer.user.js
// @version 0.02
// @version 0.03
// @description normalize url in video description / remove miniplayer (still activated when you watch in playlist)
// @author x94fujo6
// @match https://www.youtube.com/*
Expand All @@ -12,88 +12,33 @@

(function () {
'use strict';
let location;
let cid;
window.onload = setTimeout(function () {
console.log("script start");
location = window.location.href;
main();
removeMiniplayer();
setInterval(watcher, 1000);
}, 3000);

function removeMiniplayer() {
let b = document.querySelectorAll(".ytp-miniplayer-button");
if (b) b.forEach(e => { e.remove(); });
b = document.querySelectorAll("[src*='miniplayer.js']");
if (b) b.forEach(e => { e.remove(); });
}

function watcher() {
let newlocation = window.location.href;
if (location != newlocation) {
console.log(`page changed. rerun script.`);
location = newlocation;
setTimeout(() => {
main();
removeMiniplayer();
}, 3000);
}
}
window.onload = () => { setInterval(main, 500); };

function main() {
let c_location = window.location.href;
if (!c_location.includes("/watch?v=")) return;
let pos = document.getElementById("meta-contents");
let id = getVideoID();
if (id && pos) {
console.log(`start normalizer [id: ${id}]`);
cid = id;
normalizer(id, c_location);
} else {
console.log("waiting for page load.");
setTimeout(main, 100);
}
}

function normalizer(id, c_location) {
let newid = getVideoID();
if (newid != id) return console.log(`page changed. stop normalizer [id: ${id}]`);

let links = document.querySelectorAll("a.yt-formatted-string");
let count = 0;
links.forEach(a => {
if (!a.href.includes("redirect?")) return;
let url = getLink(a.href);
if (!url) return;
url = decodeURIComponent(url);
console.log(`set urlReplacer on url: ${url}`);
let interID = setInterval(() => urlReplacer(a, url, interID, c_location), 500);
a.addEventListener("mouseover", () => { urlReplacer(a, url, interID, c_location); });
count++;
});
console.log(`script end. found ${count} url`);
}

function getLink(href) {
let reg = /&q=(.[^\&]*)/;
let extract = reg.exec(href);
return extract ? extract[1] : false;
}
if (links) normalizer(links);
removeMiniplayer();

function urlReplacer(ele, url, interID, c_location) {
if (c_location != window.location.href) {
console.log(`page change. urlReplacer:${interID} will stop`);
return clearInterval(interID);
}
if (ele.href != url) {
//console.log(`link change detected. re-replace:${url}`);
ele.href = url;
function normalizer(links) {
links.forEach(a => {
if (!a.href.includes("redirect?")) return;
let url = getLink(a.href);
if (!url) return;
a.href = decodeURIComponent(url);
});

function getLink(href) {
let reg = /&q=(.[^\&]*)/;
let extract = reg.exec(href);
return extract ? extract[1] : false;
}
}
}

function getVideoID() {
let pos = document.querySelector("ytd-watch-flexy");
return pos ? pos.getAttribute("video-id") : false;
function removeMiniplayer() {
let b = document.querySelectorAll(".ytp-miniplayer-button");
if (b) b.forEach(e => { e.remove(); });
b = document.querySelectorAll("[src*='miniplayer.js']");
if (b) b.forEach(e => { e.remove(); });
}
}
})();

0 comments on commit ace18f0

Please sign in to comment.