7

我创建了一个小的命令式香草 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");
4

3 回答 3

4

要中止加载网站,您可以简单地使用该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>调用.stop()已完全评估,但未显示之后对 DOM 所做的更改.stop()

或者,要删除完整的 DOM 内容,最好重定向浏览器,该解决方案即使在页面加载后也有效:

window.open('about:blank','_self')
于 2019-11-04T22:57:40.370 回答
1

旧的,错误的答案在这里: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") 在当前窗口中打开网站。

于 2019-11-05T13:52:54.020 回答
-1

如果您以任何方式控制网站服务器,您可以代理外部脚本,甚至是内部脚本,这样当您不想被解析时,您只需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;为文件顶部的内容。

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

const script = encodeUriComponent('http://example.com/script.js')
http://myserver.com/proxy?url=script&allowed=false
于 2019-11-18T22:04:46.517 回答