[Release] ComicVine Userscript

Information and Tutorials on features in Mylar and how to use it
btx
Posts: 7
Joined: Mon Mar 02, 2020 12:28 am

Re: [Release] ComicVine Userscript

Post by btx »

The problem ended up being a lack of the run-at line in the header - none of the other changes I made had any effect. Most of you probably had your "went to run" property set to what the run-at line forces it to do. So I was able to reduce the script a bit, but being a proper engineer, I had to add functionality to balance that out. So now, rather than making you remember to configure your host / port, it now prompts you for host/port/username/password (to cover those sane individuals who password protect their services). I'll mention this works for me without the password as long as I'm coming from localhost.

I added a menu command to allow you to reset your config (in case your un/pw change, or your server relocates). To get to this menu, go to a page where the userscript is loaded, and right click on the icon that's in the upper right part of the browser. You should see a line specific to the "ComicVine: Add to Mylar" userscript, and under that, it should list "Clear Saved Configuration".

Code: Select all

// ==UserScript==
// @name        ComicVine: Add to Mylar
// @namespace   https://comicvine.gamespot.com/addtomylar/4050-
// @author      losty, btx
// @match       *://comicvine.gamespot.com/*/4050-*/
// @version     0.3.4
// @run-at      document-idle
// @grant       GM_log
// @grant       GM_addStyle
// @grant       GM_xmlhttpRequest
// @grant       GM_notification
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_deleteValue
// @grant       GM_registerMenuCommand
// @description Rewrite of the original "Add to Mylar" script.  Key features: only match the Volume 4050 pages, explicitly wait for the DOM to load before attempting to manipulate it, use Ajax req to submit, prompt / persist params.  To use: Load a comicvine Volume page.
// ==/UserScript==

(function() {
    var vardesc = [
                    {m: "What hostname or IP is your Mylar instance listening on?", d: "localhost", v: "wshost"},
                    {m: "What port is your Mylar instance listening on?", d: "8090", v: "wsport"},
                    {m: "What is the Mylar username (blank if there is none)?", d: "", v: "wsun"},
                    {m: "What is the Mylar password (blank if there is none)?", d: "", v: "wspw"}
                  ];

    var wshost=null;
    var wsport=null;
    var wsun=null;
    var wspw=null;

    function get_param(i) {
        var vd = vardesc[i];
        var msg = vd.m;
        var def = vd.d;
        var varname = vd.v;
        var initval = GM_getValue(varname, null);
        if (initval != null) {
            return initval;
        }
        msg = "[CV: Add to Mylar] " + msg;
        var v = prompt(msg, def);
        GM_setValue(varname, v);
        return v;
    }

    function get_config() {
        wshost = get_param(0);
        wsport = get_param(1);
        wsun = get_param(2);
        wspw = get_param(3);
    }

    function clearSaved() {
        var i;
        for (i=0; i<vardesc.length; ++i) {
            GM_deleteValue(vardesc[i].v);
        }
        self.location.reload();
    }
    get_config();
    var volid = window.location.pathname.split('/')[2].split('-')[1];
    var ustr = "http://" + wshost + ":" + wsport + "/addbyid?comicid=" + volid + "&calledby=True";

    GM_registerMenuCommand("Clear saved configuration.", clearSaved);

    GM_addStyle('.spinny  { animation: spinning-cog 1.3s infinite ease; }');
    GM_addStyle('@keyframes spinning-cog  { 0% { transform: rotate(0deg); } 20% { transform: rotate(-45deg); }	100% { transform: rotate(360deg);	} }');
    GM_addStyle('img.mylarload { width:38px;margin-right:-38px;position:relative;left:-50px;filter:drop-shadow(0px 0px 14px #c0c010); }');
    GM_addStyle('img.mylarload.lastgood { filter: drop-shadow(0px 0px 14px #00f010); }');
    GM_addStyle('img.mylarload.lastbad { filter: drop-shadow(0px 0px 14px #f01000); }');
    $('a.wiki-title').before($('<img class="mylarload" src="http://' + wshost + ':' + wsport + '/images/mylarlogo.png" title="Add this volume to Mylar" style="">'));
    $('img.mylarload').css({cursor: 'pointer'}).on('click', function(e) {
        $(this).addClass('spinny').removeClass('lastgood').removeClass('lastbad');
        var options={
            method: 'GET',
            url: ustr,
            data: null,
            onload: function(response) {
                $('img.mylarload').removeClass('spinny').addClass('lastgood');
                GM_log(response.responseText);
                GM_notification({text:"Successfully submitted this volume back to your Mylar server.", title: "Success", silent: true, timeout: 5000});
            },
            onerror: function(e) {
                $('img.mylarload').removeClass('spinny').addClass('lastbad');
                GM_log(e);
                GM_notification({text: "Failed to submit this volume back to your Mylar server.", title: "Failure", silent: true, timeout: 5000});
            }
        };
        if (wsun != "") {
            options.username = wsun;
        }
        if (wspw != "") {
            options.password = wspw;
        }
        GM_xmlhttpRequest(options);
        return false;
    })

})();
btx
Posts: 7
Joined: Mon Mar 02, 2020 12:28 am

Re: [Release] ComicVine Userscript

Post by btx »

Well I finally got auth working on mylar, and the specification in GM was wrong. Sorry, Fixed here:

Code: Select all

// ==UserScript==
// @name        ComicVine: Add to Mylar
// @namespace   https://comicvine.gamespot.com/addtomylar/4050-
// @author      losty, btx
// @match       *://comicvine.gamespot.com/*/4050-*/
// @version     0.3.5
// @run-at      document-idle
// @grant       GM_log
// @grant       GM_addStyle
// @grant       GM_xmlhttpRequest
// @grant       GM_notification
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_deleteValue
// @grant       GM_registerMenuCommand
// @description Rewrite of the original "Add to Mylar" script.  Key features: only match the Volume 4050 pages, explicitly wait for the DOM to load before attempting to manipulate it, use Ajax req to submit, prompt / persist params.  To use: Load a comicvine Volume page.
// ==/UserScript==

(function() {
    var vardesc = [
                    {m: "What hostname or IP is your Mylar instance listening on?", d: "localhost", v: "wshost"},
                    {m: "What port is your Mylar instance listening on?", d: "8090", v: "wsport"},
                    {m: "What is the Mylar username (blank if there is none)?", d: "", v: "wsun"},
                    {m: "What is the Mylar password (blank if there is none)?", d: "", v: "wspw"}
                  ];

    var wshost=null;
    var wsport=null;
    var wsun=null;
    var wspw=null;

    function get_param(i) {
        var vd = vardesc[i];
        var msg = vd.m;
        var def = vd.d;
        var varname = vd.v;
        var initval = GM_getValue(varname, null);
        if (initval != null) {
            return initval;
        }
        msg = "[CV: Add to Mylar] " + msg;
        var v = prompt(msg, def);
        GM_setValue(varname, v);
        return v;
    }

    function get_config() {
        wshost = get_param(0);
        wsport = get_param(1);
        wsun = get_param(2);
        wspw = get_param(3);
    }

    function clearSaved() {
        var i;
        for (i=0; i<vardesc.length; ++i) {
            GM_deleteValue(vardesc[i].v);
        }
        self.location.reload();
    }
    get_config();
    var volid = window.location.pathname.split('/')[2].split('-')[1];
    var ustr = "http://" + wshost + ":" + wsport + "/addbyid?comicid=" + volid + "&calledby=True";

    GM_registerMenuCommand("Clear saved configuration.", clearSaved);

    GM_addStyle('.spinny  { animation: spinning-cog 1.3s infinite ease; }');
    GM_addStyle('@keyframes spinning-cog  { 0% { transform: rotate(0deg); } 20% { transform: rotate(-45deg); }	100% { transform: rotate(360deg);	} }');
    GM_addStyle('img.mylarload { width:38px;margin-right:-38px;position:relative;left:-50px;filter:drop-shadow(0px 0px 14px #c0c010); }');
    GM_addStyle('img.mylarload.lastgood { filter: drop-shadow(0px 0px 14px #00f010); }');
    GM_addStyle('img.mylarload.lastbad { filter: drop-shadow(0px 0px 14px #f01000); }');
    $('a.wiki-title').before($('<img class="mylarload" src="http://' + wshost + ':' + wsport + '/images/mylarlogo.png" title="Add this volume to Mylar" style="">'));
    $('img.mylarload').css({cursor: 'pointer'}).on('click', function(e) {
        $(this).addClass('spinny').removeClass('lastgood').removeClass('lastbad');
        var options={
            method: 'GET',
            url: ustr,
            data: null,
            onload: function(response) {
                $('img.mylarload').removeClass('spinny').addClass('lastgood');
                GM_log(response);
                if (response.status == "401") {
                    GM_notification({text: "Bad username/password prevented the submission from working.", title: "Failure", silent: false});
                    return;
                }
                GM_notification({text: "(" + response.status + ") Successfully sent this volume back to Mylar.", title: "Success", silent: true, timeout: 5000});
            },
            onerror: function(e) {
                $('img.mylarload').removeClass('spinny').addClass('lastbad');
                GM_log(e);
                GM_notification({text: "Failed to submit this volume back to your Mylar server.", title: "Failure", silent: true, timeout: 5000});
            }
        };
        if ((wsun != "") && (wspw != "")) {
            options.headers = {"Authorization": "Basic " + btoa(wsun + ":" + wspw)}
        }
        GM_xmlhttpRequest(options);
        return false;
    })

})();
Post Reply