window.onload可以在Firefox + Greasemonkey脚本中使用,但不能在Chrome用户脚本中使用? [英] window.onload works in Firefox+Greasemonkey script but not in a Chrome userscript?

查看:285
本文介绍了window.onload可以在Firefox + Greasemonkey脚本中使用,但不能在Chrome用户脚本中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个网页 http://example.com/1.php 照常包含JavaScript文件:

There is a page http://example.com/1.php that includes javascript file as usual:

<script type="text/javascript" src="/util.js?1354729400"></script>

此文件包含名为 exampleFunction 的函数,我需要在我的用户脚本。
我也有一个用户脚本:

This file contain function named exampleFunction which I need to use in my userscript. Also I have an user script:

// ==UserScript==
// @name          SomeName
// @namespace     http://example.com/userscripts
// @description   Greets the world
// @include       http://example.com/*
// ==/UserScript==
window.onload = function () {
        console.log(exampleFunction);
      alert("LOADED!");
}

在Firefox中可以正常工作,并在Chrome中返回错误:

which works perfectly in Firefox and returns an error in Chrome:

Uncaught ReferenceError: exampleFunction is not defined 

如何使它工作?

推荐答案

exampleFunction 未定义是因为Chrome用户脚本在沙盒中操作(孤立的世界)。请注意,Greasemonkey脚本通常也在沙盒中运行,但您的目前运行隐式 @grant none

如果您的脚本使用 GM _ 函数,它将停止工作

The reason that exampleFunction was undefined is because Chrome userscripts operate in a sandbox ("isolated world"). Note that Greasemonkey scripts often operate in a sandbox too, but yours is currently running with an implicit @grant none.
If your script were to use a GM_ function, it would stop working in Firefox too.

要使这个脚本在两个浏览器(以及其他几个)上正常工作,请使用脚本注入 类似于此答案

To make this script work on both browsers (and a few others, as well), use Script Injection similar to this answer.

还有另一个钩子,因为那个脚本使用 window.onload Chrome用户脚本使用默认执行启动模式,通常不会看到 onload event。

However, there is another hitch, since that script is using window.onload. Chrome userscripts, with the default execution start-mode, will often never see the onload event.

要解决此问题,请添加 // @ run-at document-end 添加到元数据块。

To get around that, add // @run-at document-end to the metadata block.

所以脚本变成:

// ==UserScript==
// @name            SomeName
// @namespace       http://example.com/userscripts
// @description     Greets the world
// @include         http://example.com/*
// @run-at          document-end
// @grant           none
// ==/UserScript==

function GM_main () {
    window.onload = function () {
        console.log(exampleFunction);
        alert("LOADED!");
    }
}

addJS_Node (null, null, GM_main);

//-- This is a 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);
}

这篇关于window.onload可以在Firefox + Greasemonkey脚本中使用,但不能在Chrome用户脚本中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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