简体   繁体   English

在文档加载到JS之前调用函数

[英]Calling a function before the document loads up in JS

My friend asks me to do this. 我的朋友要我这样做。

He needs to prevent his children from going to certain websites. 他需要阻止自己的孩子访问某些网站。

He has tamper monkey installed with chrome. 他为猴子安装了铬合金。 I have to make a script in tamper-monkey so that when it reaches the website it will change the web content. 我必须用防篡改脚本编写脚本,以便当它到达网站时会更改Web内容。

The source code is: 源代码是:

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://www.harmful-website.com/*
// @copyright  2012+, You
// ==/UserScript==
document.write("<b>You are not allowed to visit this site</b>");

This script works only after the web-site is loaded full. 仅在网站完全加载后,此脚本才有效。 But his children stop the loading of website in the middle and they are able to view a part of it. 但是他的孩子们在中间停止了网站的加载,他们能够查看其中的一部分。

Even document.onload=function(){document.write("...");} works after load. 甚至在加载后也可以使用document.onload=function(){document.write("...");} Are there any way to make the script run before the document is loaded ie Immediately after the web address is typed on the address bar or hyperlink is clicked. 有什么方法可以使脚本在文档加载之前运行,即在地址栏上键入Web地址或单击超链接之后立即运行。

Your code will work, you just need to set @run-at document-start Doc , like so: 您的代码将起作用,您只需要设置@run-at document-start Doc即可 ,如下所示:

// ==UserScript==
// @name        site blocker
// @match       http://www.harmful-website.com/*
// @run-at      document-start
// ==/UserScript==

document.write ("<b>You are not allowed to visit this site</b>");


Important: 重要:

  1. This will work, for now , on Chrome and with Tampermonkey, but it does not work on other browsers. 目前 ,该功能在Chrome和Tampermonkey上仍然可以使用,但在其他浏览器上则无法使用。 For example, on Firefox, the document.write call will throw the error: 例如,在Firefox上, document.write调用将引发错误:

    Error: The operation is insecure. 错误:操作不安全。

    (But the page will still be completely blank.) (但是页面仍将完全空白。)

  2. Although a userscript like this will work (mostly); 尽管这样的用户脚本可以工作(大多数情况下); it is a klugey, brittle, low performance approach and is easily defeated. 这是一种笨拙,脆弱,低性能的方法,很容易被击败。 Here are just a few ways that are better, faster, easier, and harder for savvy kids to kibosh: 对于精明的孩子来说,以下是一些更好,更快,更容易和更困难的方法:

    • Use your home router's siteblock and/or parental controls. 使用家用路由器的站点阻止和/或家长控制。 Almost every router and/or modem has this feature, these days. 如今,几乎每个路由器和/或调制解调器都具有此功能。
    • Install one of the many extensions that do this kind of blocking. 安装执行此类阻止的许多扩展之一。
    • Block the site with an Adblock rule. 使用Adblock规则屏蔽网站。
    • Block the site with the machine's hosts file . 使用机器的hosts文件阻止该站点
    • Use a proxy server. 使用代理服务器。
    • Search on Super User for more options. 超级用户上搜索更多选项。

Just call the function right after you define it. 定义函数后立即调用该函数。 Like in the header or somehwere. 就像在标题中一样。

However, you need to consider if the function has actually everything it requires, like HTML elements on the page if it access any of them, as those won't necessarily be loaded when calling your function. 但是,您需要考虑该函数是否真正具有所需的一切,例如页面上的HTML元素(如果可以访问其中的任何元素),因为在调用函数时不一定会加载这些元素。

HTML file is parsed row by row. HTML文件逐行解析。 So if your write your < script > just after < html > tag it will be executed before anything else. 因此,如果您在<html>标记之后编写<脚本>,它将首先执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM