Firefox:在网站上禁用 window.location

Tel*_*nor 5 firefox javascript redirection security-policy

我试图阻止某个站点能够使用 javascript 将浏览器重定向到另一个页面。有问题的脚本是内联脚本,因此 Greasemonkey 和 adBlock 对此无能为力。

可配置的安全策略 (CAPS) 似乎是答案,但我无法使用它,而且我window.location所有的搜索都没有发现任何有用的东西。该脚本如下所示:

<script>
        window.location = "someotherpage.html";
</script>
Run Code Online (Sandbox Code Playgroud)

这就是我在 user.js 文件中尝试过的:

user_pref("capability.policy.policynames", "noredirect");
user_pref("capability.policy.noredirect.sites", "http://www.zshare.net http://127.0.0.1");        
user_pref("capability.policy.noredirect.Window.Location.replace", "noAccess");
user_pref("capability.policy.noredirect.Window.Location.assign", "noAccess");
user_pref("capability.policy.noredirect.Window.Location.reload", "noAccess");
user_pref("capability.policy.noredirect.Window.Location", "noAccess");
user_pref("capability.policy.noredirect.Document.Location.replace", "noAccess");
user_pref("capability.policy.noredirect.Document.Location.assign", "noAccess");
user_pref("capability.policy.noredirect.Document.Location.reload", "noAccess");
user_pref("capability.policy.noredirect.Document.Location", "noAccess");
user_pref("capability.policy.noredirect.Location.replace", "noAccess");
user_pref("capability.policy.noredirect.Location.assign", "noAccess");
user_pref("capability.policy.noredirect.Location.reload", "noAccess");
user_pref("capability.policy.noredirect.Location", "noAccess");
Run Code Online (Sandbox Code Playgroud)

我一直在本地托管的页面上对其进行测试,并且能够阻止警报功能,但我尝试的任何内容都无法禁用window.location.

有谁知道如何做到这一点?

XP1*_*XP1 0

虽然 Firefox 无法解决您的问题,但是替代解决方案怎么样?

在 Opera 中,您只需要一个简单的正则表达式即可查找并替换脚本。没有复杂的扩展,只是一个简单的用户 JS 文件,如下所示。

Disable redirection 1.00.js:

// ==UserScript==
// @name Disable redirection
// @version 1.00
// @description Disables redirection.
// @namespace http://superuser.com/questions/353339/firefox-disable-window-location-on-website/511703#511703
// @copyright 2012
// @author XP1
// @homepage https://github.com/XP1/
// @license Apache License, Version 2.0; http://www.apache.org/licenses/LICENSE-2.0
// @include http*://example.com/*
// @include http*://*.example.com/*
// ==/UserScript==

/*
 * Copyright 2012 XP1
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jslint browser: true, vars: true, maxerr: 50, indent: 4 */
(function (opera) {
    "use strict";

    var isReplaced = false;

    function replaceJs(userJsEvent) {
        if (isReplaced) {
            return;
        }

        var element = userJsEvent.element;
        element.text = element.text.replace(/window\.location = "someotherpage\.html";/g, "");

        isReplaced = true;
    }

    opera.addEventListener("BeforeScript", function listener(userJsEvent) {
        if (isReplaced) {
            opera.removeEventListener("BeforeScript", listener, false);
            return;
        }

        replaceJs(userJsEvent);
    }, false);
}(this.opera));
Run Code Online (Sandbox Code Playgroud)