Diskusie » Vývoj

gm_getvalue/setvalue is not defined?

§
Pridaný: 25.02.2019
Upravený: 26.02.2019

gm_getvalue/setvalue is not defined?

// ==UserScript==
// @name     converto.io click the download link
// @include    *://*.converto.io/en/download/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM.getValue
// @grant    GM.setValue
// @run-at document-end
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */

setTimeout(main, 1000);

function main(){
            waitForKeyElements ("#download-url", clickNode, true);
}

function clickNode (jNode) {
    var lastconv = gm_getvalue("lastcv",false);
    var yt = window.location.href;
    var conv = yt.match(/id=.*/);
    var oout = lastconv + "<-lastconv____conv->" + conv;
    alert(oout);
    if( conv ){
        if( lastconv != conv ){
        document.getElementById("download-url").click();
        gm_setvalue("lastcv",conv);
        }
    }
}

The above script is simply waiting for a link show up and click it. The problem is after clicking, my download tool will make url redirect and download begins and redirect back, make this script triggered again and click again, thus an infinite loop. My first attempt is to use cookies, I append part of url to the cookie, and this script will stop clicking link if that part is already in cookie. This method I did well in another script, but I failed this one, don't know why yet. Now I try 2nd method: gm_setvalue/getvalue method, as shown in the code, but I get "undefined" error for them both. I've googled and tried wrapping it like:

setTimeout(function(){
      GM_setValue(‘foo’,true);
      GM_etValue(‘foo’);  
    },0)

Still get "undefined". I also tried 3rd method: window.postmessage and addeventlistener, but too complicated for me, failed. Don't know what to do now, could anybody please give me some help?

woxxomZablokovaný
§
Pridaný: 26.02.2019
Upravený: 26.02.2019
  • You're granting GM.getValue but you're trying to use GM_getValue - see the difference?
  • JavaScript is case-sensitive so you can't use gm_setvalue or gm_getvalue - it should be GM_setValue and GM_getValue
  • You have a typo: GM_etValue

If your script manager is Tampermonkey, simply replace . with _ in both declarations.

If your script manager is Greasemonkey 4, you'll need to rework the code a bit as shown in the documentation.

§
Pridaný: 26.02.2019

@wOxxOm 说道:

  • You're granting GM.getValue but you're trying to use GM_getValue - see the difference?
  • JavaScript is case-sensitive so you can't use gm_setvalue or gm_getvalue - it should be GM_setValue and GM_getValue
  • You have a typo: GM_etValue

If your script manager is Tampermonkey, simply replace . with _ in both declarations.

If your script manager is Greasemonkey 4, you'll need to rework the code a bit as shown in the documentation.

OMG, today I learned so much and assembled many versions, just did not realize these. Thanks so much for your help!

§
Pridaný: 27.02.2019
Upravený: 27.02.2019

......

§
Pridaný: 27.02.2019

@wOxxOm Learned a lot from this case. Till now the only explanation I could think of is, allow me to make it clear: I thought it's a 2 urls case, but it's about 3 urls --- when link clicked, page url turns into that link's url, then jump to 3rd url - captured by a downloader with help of a chrome extension thus the url is like "chrome-extension://xxxxx", then jump back to link's url ( I was thinking it will jump back to the very first url, but lots of tests showed no matter what conditions I added( such of cookie or GM Value) to prevent infinite downloading, it will jump to 3rd url again and again, so a 2nd url must be the culprit, 'coz my condition is on 1st url).

Here is a working solution. Any improvement please let me know. [code]// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @run-at document-end // ==/UserScript== //- The @grant directives are needed to restore the proper sandbox. /* global $, waitForKeyElements */ var flag = null; setTimeout(main, 1000);

function main() { // wait for link appears waitForKeyElements("#download-url", clickNode, true); }

function clickNode(jNode) { // wait for link unhide flag = setInterval(opt, 500); }

function opt() { var yc = $("#download-url").is(":hidden") ? "not there" : "appear"; console.log(yc); if (yc == "appear") { clearInterval(flag); var url = document.getElementById("download-url").href; // here for download I open new window/tab openWin(url); } }

function openWin(url) { $('body').append($('')) document.getElementById("openWin").click(); $('#openWin').remove(); }[/code]

§
Pridaný: 27.02.2019

ooops, the last function should be:

Pridať odpoveď

Aby ste mohli pridať odpoveď, prihláste sa.