GM_getValue not defined (in the entered code)?

I saw GM_getValue undefined The error , however, I gave GM_getValueand GM_setValueand define default values.

Code example:

// ==UserScript==
// @name        SO_test
// @include     https://stackoverflow.com/*
// @version     1
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

// Get jQuery thanks to this SO post:
// https://stackoverflow.com/a/3550261/2730823
function addJQuery(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
    script.addEventListener('load', function() {
        var script = document.createElement("script");
        script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
}

function main() {
$("#wmd-input").on("contextmenu", function(e) {
    e.preventDefault();
    console.log("GM_getValue: " + GM_getValue("extra_markdown", True));
});
}
addJQuery(main);

If you right-click the “add response” text box on SO after installing the above example, FF says GM_getValue is undefinedin the console. Why is this?

How do I make GM functions work?

+4
source share
1 answer

, script GM_getValue() ( ); .
, :
GM_xmlhttpRequest Injected Code?

GM_ Greasemonkey , ?
GM_.

, script jQuery. . - ( ). , Greasemonkey ( Tampermonkey), script: , , :

// ==UserScript==
// @name    SO_test
// @include https://stackoverflow.com/*
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant   GM_getValue
// @grant   GM_setValue
// ==/UserScript==

$("#wmd-input").on ("contextmenu", function (e) {
    e.preventDefault ();

    //-- Important: note the comma and the correct case for `true`.
    console.log ("GM_getValue: ", GM_getValue ("extra_markdown", true) );
});
+8

Source: https://habr.com/ru/post/1547663/


All Articles