Skip to content

Instantly share code, notes, and snippets.

@myjian
Last active August 18, 2018 23:13
Show Gist options
  • Save myjian/198222f5272dcdc9e43d897c7a91852d to your computer and use it in GitHub Desktop.
Save myjian/198222f5272dcdc9e43d897c7a91852d to your computer and use it in GitHub Desktop.
Intelligently get password and play video for hdx3
/**
* This is a set of user scripts that open direct link to video for
* hdx3 blog when user hits the "確定" button next to the password input.
*
* Installation:
* 1. Install Tampermonkey browser extension.
* 2. Create a new script. Copy and paste PART 1 and save.
* 3. Create another script. Copy and paste PART 2 and save.
* 4. Restart the browser, OR reload any existing hdx3 pages.
*
* What the scripts do:
* 1. read password from blog post
* 2. use the password to get direct link to the video
* 3. redirect the page to that link
*/
/* PART 1: hdx3.blogspot.com */
// ==UserScript==
// @name hdx3 pass password to iframe
// @version 0.1
// @description try to take over the world!
// @author Ming-Yuan Jian
// @match https://hdx3.blogspot.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log("wave hdx3");
window.addEventListener("message", (e) => {
if (e.data.locationAssign) {
window.location.assign(e.data.locationAssign);
}
if (e.data !== "getPasswd") {
return;
}
const postBody = document.querySelector(".post-body.entry-content").innerText;
const passwdMatch = postBody.match(/密碼:\s*([0-9]+)/);
if (passwdMatch) {
e.source.postMessage({passwd: passwdMatch[1]}, e.origin);
}
});
})();
/* PART 2: vlog.xuite.net */
// ==UserScript==
// @name Xuite get video url
// @version 0.2
// @description try to take over the world!
// @author Ming-Yuan Jian
// @match https://vlog.xuite.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log("wave vlog");
let scriptDisabled = false;
let textarea, copyButton, postMessageTimeout;
function postMessageToParent(msg) {
window.parent.postMessage(msg, "https://hdx3.blogspot.com/");
}
function prepareOutputArea() {
if (!textarea) {
const outputSpan = document.getElementById("single-panel-password-label");
outputSpan.style.display = "block";
textarea = outputSpan.children[0];
textarea.style.display = "block";
textarea.style.margin = "0 auto"
textarea.style.width = "80%";
copyButton = outputSpan.children[1];
copyButton.type = "button";
copyButton.style.display = "block";
copyButton.style.margin = "3px auto 10px";
copyButton.style.fontSize = "20px";
copyButton.style.padding = "5px 14px";
copyButton.value = "Copy video link";
}
}
function step1_handleEvent(e) {
if (scriptDisabled) { return; }
if (textarea && copyButton && e.target === copyButton) {
textarea.select();
document.execCommand('copy');
}
const submitButton = document.getElementsByClassName("single-panel-password-submit")[0];
if (e.target !== submitButton) {
return;
}
if (window.top !== window.self) { // iframed. Try to get password from the post!
postMessageToParent("getPasswd");
postMessageTimeout = window.setTimeout(step2_handleSubmit, 160);
} else {
step2_handleSubmit();
}
e.preventDefault();
e.stopPropagation();
}
function step1b_handleMessage(e) {
if (e.data.passwd) {
window.clearTimeout(postMessageTimeout);
const passwd = e.data.passwd;
document.getElementById("single-panel-password-text").value = passwd;
step2_handleSubmit(passwd);
}
}
function step2_handleSubmit(passwd) {
if (passwd) {
console.log("got password from postMessage");
} else {
console.log("get password from text input");
}
passwd = passwd || document.getElementById("single-panel-password-text").value;
const mediaId = self.mediaInfo ? self.mediaInfo.MEDIA_ID : document.getElementById("play-hiddendata").dataset.mediaid;
self.jQuery.ajax({
url: "https://vlog.xuite.net/_ajax/default/media/ajax",
type: "post",
dataType: "json",
data: {
act: "checkPasswd",
mediumId: mediaId,
passwd: passwd
},
error: function(a) {},
success: step3_handleSuccess
});
};
function step3_handleSuccess(res) {
if (!res.success) {
console.log("password might be wrong.");
document.getElementById("single-panel-password-text").focus();
return;
}
res = res.media;
let videoUrl = res.html5HQUrl2 || res.html5HQUrl || res.html5Url;
videoUrl = window.location.protocol + videoUrl;
// Usage 1: Just open the link
if (window.top !== window.self) { // iframed. Let the parent open the link!
postMessageToParent({locationAssign: videoUrl});
} else {
window.location.assign(videoUrl);
}
// Usage 2: Show the link and a button to copy the link
//console.log(videoUrl);
//prepareOutputArea();
//textarea.value = videoUrl;
}
document.addEventListener('click', step1_handleEvent, true /* useCapture */);
window.addEventListener('message', step1b_handleMessage);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment