如何更改greasemonkey中的var值

时间:2013-06-27 23:39:10

标签: greasemonkey

如何更改代码中的var值。我已经尝试但没有成功

<script type="text/javascript" charset="utf-8">
var i9 = '071c57';
var plusActive = false;
</script>

到这个

<script type="text/javascript" charset="utf-8">
var i9 = '071c57';
var plusActive = true;
</script>

我尝试使用以下代码但未成功

// ==UserScript==
// @name          PLUS 
// @namespace     http://userstyles.org
// @description   PLUS 
// @author        md
// @homepage      http://userstyles.org/styles/43691
// @include       http://azet.sk/*
// @include       https://azet.sk/*
// @include       http://-azet.sk/*
// @include       https://-azet.sk/*
// @include       http://*.azet.sk/*
// @include       https://*.azet.sk/*
// @include       http://*-azet.sk/*
// @include       https://*-azet.sk/*
// ==/UserScript==

myInfo
{
"plusActive":true;
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确,这应该是一个简单的解决办法。

您只需覆盖变量:

所以替换

myInfo
{
"plusActive":true;
}

plusActive = true;

你应该不需要运行任何类型的函数,假设在页面加载之前不会读取有问题的变量(这远非确定)。

答案 1 :(得分:0)

由于plusActive是目标页面范围的全局(不是脚本范围)...

对于Firefox ,这将起作用:

// ==UserScript==
// @name          PLUS 
// @namespace     http://userstyles.org
// @description   PLUS 
// @author        md
// @homepage      http://userstyles.org/styles/43691
// @include       http://azet.sk/*
// @include       https://azet.sk/*
// @include       http://-azet.sk/*
// @include       https://-azet.sk/*
// @include       http://*.azet.sk/*
// @include       https://*.azet.sk/*
// @include       http://*-azet.sk/*
// @include       https://*-azet.sk/*
// @grant         none
// ==/UserScript==

window.plusActive = true;

对于此脚本,@grant none对于确保始终预期的操作非常重要。


对于跨浏览器方法,请使用脚本注入:

// ==UserScript==
// @name          PLUS 
// @namespace     http://userstyles.org
// @description   PLUS 
// @author        md
// @homepage      http://userstyles.org/styles/43691
// @include       http://azet.sk/*
// @include       https://azet.sk/*
// @include       http://-azet.sk/*
// @include       https://-azet.sk/*
// @include       http://*.azet.sk/*
// @include       https://*.azet.sk/*
// @include       http://*-azet.sk/*
// @include       https://*-azet.sk/*
// @grant         GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

addJS_Node ('plusActive = true;');

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}