Skip to content

Instantly share code, notes, and snippets.

@dandelionred
Last active September 2, 2020 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandelionred/8c6059be4b2fdc55bb990baea8298a67 to your computer and use it in GitHub Desktop.
Save dandelionred/8c6059be4b2fdc55bb990baea8298a67 to your computer and use it in GitHub Desktop.
[tampermonkey] vk image save
<?php
/**
* Decode base64 encoded string with '+/=' replaced with '._-'.
* @param type $s
* @return type
*/
function base64Xd($s) {
return base64_decode(strtr($s, '._-', '+/='));
}
/**
* Convert $http_response_header into k=>v map.
* @param Array $list
* @return Array
*/
function mapHeaders($list) {
return array_reduce(array_slice($list, 1), function($carry, $item) {
list($k, $v) = explode(':', $item, 2);
$carry[strtolower($k)] = ltrim($v);
return $carry;
}, []);
}
function main() {
ini_set('display_errors', 0);
if (isset($_GET['url'])) {
$url = base64Xd($_GET['url']);
if ($url !== false) {
$urlParts = parse_url($url);
if ($urlParts !== false && ($urlParts['scheme'] === 'http' || $urlParts['scheme'] === 'https')) {
$filename = basename($urlParts['path']);
if (strlen($filename)) {
$response = file_get_contents($url, false, stream_context_create([
'http' => [
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'timeout' => 10,
],
]));
if ($response !== false) {
$headers = mapHeaders($http_response_header);
if (isset($headers['content-type'])) {
header('Content-Type: ' . $headers['content-type']);
header("Content-Disposition: attachment; filename*=UTF-8''" . urlencode($filename));
echo $response;
}
}
}
}
}
}
}
main();
// ==UserScript==
// @name vk image save
// @namespace http://tampermonkey.net/
// @version 0.8.1
// @downloadURL https://gist.githubusercontent.com/dandelionred/8c6059be4b2fdc55bb990baea8298a67/raw/vk-image-save.user.js
// @updateURL https://gist.githubusercontent.com/dandelionred/8c6059be4b2fdc55bb990baea8298a67/raw/vk-image-save.user.js
// @match https://m.vk.com/*
// ==/UserScript==
(function () {
'use strict';
let location = null;
const html = `
<style>
#down-button {
position: absolute;
bottom: 0;
right: 0;
z-index: 2;
text-align: right;
}
#down-button a {
display: inline-block;
line-height: 48px;
padding: 0 24px;
color: lightgray;
font-weight: bold;
font-family: monospace;
}
</style>
<div id="down-button"></div>
`;
function log(s) {
console.log("[" + (new Date()).getTime() + "] " + s);
}
/**
* Convert unicode string to utf-8, base64 encode, replace "+/=" with "._-".
* @param {String} s
* @returns {String}
*/
function base64X(s) {
const pairs = {
"+": ".",
"/": "_",
"=": "-"
};
return window.btoa(unescape(encodeURIComponent(s))).replace(/[+/=]/g, m => pairs[m]);
}
function main() {
const z_photoview = document.getElementById("z_photoview");
if (!z_photoview) {
location = null;
return;
}
const img = z_photoview.querySelector("#zpv_center img.zpv_img");
if (!img || img.src === location || img.src.slice(-10) === "/blank.gif") {
return;
}
let downButton = document.getElementById("down-button");
if (!downButton) {
z_photoview.querySelector(".zpv_bottom_body").insertAdjacentHTML("beforeend", html);
downButton = document.getElementById("down-button");
}
location = img.src;
if (!downButton.firstChild) {
let a = document.createElement("a");
a.innerText = "save";
downButton.appendChild(a);
}
downButton.firstChild.href = "http://local.download1.net:81/convert.php?url=" + base64X(img.src);
}
setInterval(main, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment