Javascript变量范围在JS URI中,或如何写页面范围对象? [英] Javascript variable scope in a JS URI, or how to write page-scope objects?

查看:224
本文介绍了Javascript变量范围在JS URI中,或如何写页面范围对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Greasemonkey脚本,我想在Chrome和Firefox中使用。我知道你不能像Chrome中那样在Chrome中使用 unsafewindow ,所以我一直在尝试使用jS-uris,如下所示:Greasemonkey,Chrome和unsafeWindow.foo()

  location.assign( javascript:var tutu ='oscar';); 
location.assign(alert('1:'+ tutu););
alert('2:'+ tutu);

我收到一个错误,显示tutu未定义。显然我不明白这些变量的范围。我需要为我的工作做全局函数和变量。



编辑:实际上我认为问题不是我想要的(除非它是,在这种情况下请告诉我)我正在编写脚本的网页正在使用URLS来阻止它们去除相对URI之外的任何东西。

解决方案

在Chrome中, location.assign 调用(它们可能在单独的线程中运行)之后出现延迟,因此 tutu alert()触发并且整个代码引发异常时,c> var未定义。 (另外,如三位一样, alert 需要以 javascript:开头。)



您可以使用这样的定时器:

  location.assign(javascript:var tutu ='oscar';); 
setTimeout(
function(){location.assign(javascript:console.log('1:'+ tutu););}
,666
);

但是,我想你会同意这是一个麻烦。



尝试使用 location.assign 执行页面范围JavaScript将会使除最短/最简单的代码之外的所有内容变得麻烦。



使用脚本注入设置,重置和/或运行

  function setPageScopeGlobals(){
window.tutu ='Oscar';
window.globVar2 ='Wilde';
window.globVar3 ='Boyo';

alert('1:'+ tutu);

window.useGlobalVar = function(){
console.log(globVar2:,globVar2);
};
}

// - 设置全局变量
addJS_Node(null,null,setPageScopeGlobals);

// - 调用全局函数
addJS_Node(useGlobalVar(););

// - 标准的ish效用函数:
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);
}


I'm writing a Greasemonkey script that I'm trying to use in Chrome and Firefox. I know you can't use unsafewindow in Chrome like you can in Firefox, so I've been attempting to use jS-uris like in the answer for: Greasemonkey, Chrome and unsafeWindow.foo().

when I try the following:

location.assign("javascript:var tutu = 'oscar';");
location.assign("alert('1:' + tutu);");
alert('2:' + tutu);

I receive an error showing that "tutu" is undefined. Obviously what I'm not understanding is the scope of these variables. I need to make global functions and variables for what I'm working on. What am I not doing correctly?

EDIT: Actually I think the problem isn't what I'm trying (unless it is, in which case please tell me) but the page I'm writing the script for looks is messing with URLS to keep them from going to anything but a relative URI.

解决方案

In Chrome, there is a delay after the location.assign calls (they may run in separate threads), so the tutu var isn't defined when the alert() fires and the whole code throws an exception. (Also, as trinity said, the alert needs to be prefaced with javascript:.)

You could use a timer like so:

location.assign("javascript:var tutu = 'oscar';");
setTimeout (
    function () {location.assign("javascript:console.log('1:' + tutu);"); }
    , 666
);

But, I think you'll agree that's a spot of bother.

Trying to do page-scope javascript with location.assign will get cumbersome for all but the shortest/simplest code.

Set, reset, and/or run lots of page-scope javascript using Script Injection:

function setPageScopeGlobals () {
    window.tutu     = 'Oscar';
    window.globVar2 = 'Wilde';
    window.globVar3 = 'Boyo';

    alert ('1:' + tutu);

    window.useGlobalVar = function () {
        console.log ("globVar2: ", globVar2);   
    };
}

//-- Set globals
addJS_Node (null, null, setPageScopeGlobals);

//-- Call a global function
addJS_Node ("useGlobalVar();");

//-- Standard-ish utility function:
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);
}

这篇关于Javascript变量范围在JS URI中,或如何写页面范围对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆