Skip to content

Instantly share code, notes, and snippets.

@da0c
Last active October 24, 2018 04:49
Show Gist options
  • Save da0c/ed975341177420805ad6e54deff0af1d to your computer and use it in GitHub Desktop.
Save da0c/ed975341177420805ad6e54deff0af1d to your computer and use it in GitHub Desktop.
Autoupdate EzShare web GUI on new photo
// ==UserScript==
// @name EzShare Autoupdate
// @namespace http://tampermonkey.net/
// @version 0.181023
// @description Autoupdate EzShare web GUI on new photo
// @author artniko@gmail.com
// @match http://ezshare.card/*
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @grant GM.getValue
// @grant GM.setValue
// ==/UserScript==
(function() {
'use strict';
if (document.getElementById("srcPic") === null) {
return;
}
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : false;
}
function setCookie(name, value, options) {
options = options || {};
options.expires = "Tue, 19 Jan 2038 03:14:07 GMT";
options.path = "/";
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
var counter = 0;
var wait_limit = 500;
var errors_limit = 100;
var check_interval = 500; //ms
var updates_started = true;
//stop_updates = localStorage.getItem("stop_updates");
updates_started = getCookie("updates_started");
var toggle_link = '<a href="#" id="toggle">Stop</a>'
//document.body.innerHTML += '<br><br><br><br><div class="log" id="log" style = "position: absolute; bottom: 0; width: 80%">Autoupdate waiting...</div>';
var tr = document.getElementById("picInfo").insertRow(1);
var td = tr.insertCell(0);
td.innerHTML += '<div class="log" id="log" align = "center" style = "position: absolute; bottom: 0; font-size: 14px; width: 50%">Autoupdate waiting...</div>';
function toggle_updates() {
updates_started = !updates_started;
if (updates_started) {
start_updates();
}
else {
stop_updates1();
}
}
function start_updates() {
counter = 0;
setCookie("updates_started", updates_started);
toggle_link = '<a href="#" id="toggle">Stop ' + getCookie("updates_started")+ '</a>';
check_updates();
}
function stop_updates1() {
setCookie("updates_started", updates_started);
toggle_link = '<a href="#" id="toggle">Start ' + getCookie("updates_started")+ ' </a>';
}
if (updates_started) {
start_updates();
}
else {
stop_updates1();
}
function update_footer(msg) {
document.getElementById("log").textContent = msg + ' ' + updates_started + ' ' + getCookie("updates_started");
document.getElementById("log").innerHTML += " " + toggle_link;
document.getElementById("toggle").onclick = toggle_updates;
}
update_footer("Autoupdate waiting...");
function linkify(text) {
//var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var urlRegex = /(\b(preview)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = text.match(urlRegex);
return links;
}
function check_link(link) {
return link.search("fname=") >= 0;
}
function proc_responce(resp_txt){
var links = linkify(resp_txt);
var pic_link = links.find(check_link);
var picname_re = /fname=([-_A-Z0-9]+)\.jpg/ig;
var our_picname = document.getElementById("srcPic").src.match(picname_re)[0];
var new_picname = pic_link.match(picname_re)[0];
if (our_picname != new_picname){
location.assign('http://ezshare.card/' + pic_link);
}
}
function process_error(response) {
update_footer("Autoupdate not connected... " + counter.toString());
if (counter < errors_limit){
setTimeout(check_updates, check_interval);
}
else {
//updates_started = false; //???
update_footer("Autoupdate stoped (no connection)");
stop_updates1()
}
counter += 1;
}
function check_updates() {
if (!updates_started) {
counter = wait_limit + 1;
}
GM.xmlHttpRequest({
method: "GET",
//url: "http://127.0.0.1:5000/st/cat.html/",
url: "http://ezshare.card/photo?vtype=0&fdir=101EOS5D&dirpage=1&ftype=0&devw=320&devh=356&folderFlag=0",
onload: function(response) {
proc_responce(response.responseText);
update_footer("Autoupdate waiting... " + counter.toString());
if (counter < wait_limit) {
setTimeout(check_updates, check_interval);
}
else {
//updates_started = true;
update_footer("Autoupdate stoped (no activity)");
stop_updates1();
}
counter += 1;
},
onerror: process_error,
onabort: process_error,
ontimeout: process_error
});
}
//check_updates();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment