禁止每个网站域解析所有或某些DOM内容 文件中的代码 Tampermonkey中的文件调用

我创建了一个小的命令式原始JavaScript脚本来阻止分散注意力的新闻网站,让我感到上瘾的行为是:

// ==UserScript==
// @name         blocksite
// @match        *://*.news_site_1.com/*
// @match        *://*.news_site_2.com/*
// ==/UserScript==

function blocksite () {
    document.body.innerHTML =`<div dir="ltr"; style="font-size:100px; font-weight: bold; text-align: center">Blocked !</div>`;
}

setTimeout( blocksite(),1000)
setTimeout( blocksite(),5000) // Block possible later DOM mutations;
setTimeout( blocksite(),10000) // Block possible later DOM mutations;

该脚本基本上可以正常工作(弹出窗口会接管DOM),但是我的问题是,仅在解析和呈现了所有DOM内容之后,它才会阻止站点,而我通常希望阻止解析。

虽然收听load事件为时已晚,但收听早期的DOMContentLoaded事件可能比收听load或收听setTimeout()的结果更好,因为内容解析后,而不是呈现后,可能会发生阻塞。
但是,我需要一种完全阻止对相关网站的任何网页进行解析的方法(或者,在解析第一个DOM HTML元素节点之后,阻止任何进一步的解析)。

我尝试过的

每条评论,我都在Google Chrome浏览器中尝试过

window.stop();我不记得有什么重大变化
window.close();仅在devtool控制台上对我有用
window.location.replace("about:blank");它仅对load事件完成后才起作用,而不是在解析开始时起作用

我的问题

使用最新的ECMAScript(10)甚至不需要执行该操作,如果需要,应该使用什么命令?


Sxribe更新:

亲爱的Sxribe,我用以下代码创建了以下文件。
该文件确实是由Tampermonkey加载的(带有正确的@match列表),但是加载匹配的网站时,我发现浏览器没有任何变化(这些网站没有被阻止且无法正常加载)。

文件中的代码

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

Tampermonkey中的文件调用

window.open("C:\Users\MY_USER_NAME\Desktop\blocksite.html");
zqx9665 回答:禁止每个网站域解析所有或某些DOM内容 文件中的代码 Tampermonkey中的文件调用

要中止网站加载,只需使用window.stop()方法。

被调用时,当当前脚本运行完毕时,网站的解析将完全停止。

例如:

<p>Before scripts</p>
<script>
  document.write('<p>Before stop</p>')
  console.log('Before stop')

  window.stop()

  document.write('<p>After stop</p>')
  console.log('After stop')
</script>
<p>Between scripts</p>
<script>
  console.log('Second script')
  document.write('<p>Second script</p>')
</script>
<p>After scripts</p>

上面的HTML显示:

Before scripts
Before stop

,同时您可以在控制台中看到以下内容:

Before stop
After stop

这表明调用<script>的{​​{1}}得到了充分评估,但是未显示.stop()之后对DOM所做的更改。

或者,要擦除全部DOM内容,最好重定向浏览器,该解决方案即使在页面加载后也可以使用:

.stop()
,

错误的旧答案在这里:https://hastebin.com/vujuduforu.txt

好的,因此chrome / firefox不喜欢使用window.close()打开本地文件。最后,我最终只是将网站托管在glitch.com上(免费100%),然后重定向到该网站。这是我的所有代码:

Tampermonkey脚本:

// ==UserScript==
// @name         blocktest
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        *://*.bing.com/*
// ==/UserScript==

(function() {
    'use strict';
    window.open("https://block-sxribe.glitch.me/","_self");
})();

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>This site has been blocked.</title>
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div class="flex-container">
        <div class="content">
            <h1>This site has been blocked.</h1>
            <p id="sep"></p>
            <p>This site was blocked with Tampermonkey.</p>
        </div>
    </div>
</body>
</html>

CSS:

@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');

* {
    margin: 0;
    padding: 0;
    font-family: 'Open Sans',sans-serif;
    text-transform: uppercase;
}

.flex-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: center;
}

#sep {
    margin-left: 20vw;
    width: 60vw;
    border-bottom: 1px solid lightgray;
}

tl; dr在线托管网站,运行window.open(“ website location.com”,“ _self”)在当前窗口中打开网站。

,

如果您以任何方式控制网站服务器,则可以代理外部脚本,甚至是内部脚本,这样,当您不希望对其进行解析时,只需在该服务器上添加return;每个代理脚本的顶部。

通过代理,我的意思是:

  1. 您有一个脚本,http://example.com/script.js
  2. 您在应用程序中创建了一个路径,该路径从服务器加载http://example.com/script.js并返回结果。
  3. 如果您不希望解析http://example.com/script.js,请在将http://example.com/script.js添加为文件顶部之后,返回一个空脚本或返回return;的内容。
  4. li>

代理的路径/路由可能类似于:

const script = encodeUriComponent('http://example.com/script.js')
http://myserver.com/proxy?url=script&allowed=false
本文链接:https://www.f2er.com/3163739.html

大家都在问