如何覆盖window.screen? [英] How do I override window.screen?

查看:100
本文介绍了如何覆盖window.screen?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的PhantomJS应用程序,用于搜索寻找模式的特定页面。在其中一个页面中,该网站的所有者正在阅读 window.screen 宽度/高度属性并更改我的URL(这取消了其余部分的加载)这页纸)。设置 viewportSize 会更改窗口的宽度/高度值,但不会更改屏幕对象的宽度/高度值。

I'm writing a simple PhantomJS app that scrapes particular pages looking for patterns. In one of these pages, the owner of the site is reading the window.screen width/height properties and changing the URL on me (which is cancelling the loading of the rest of the page). Setting the viewportSize changes the width/height values of the window, but not of the screen object.

我的第一步是禁用导航,如下所示:

My first step was disabling navigation, like so:

tab.onLoadStarted = function () {
    tab.navigationLocked = true;
};

但是,因为此检查是在< head> ,它导致加载事件被触发,有时页面尚未完全加载(这导致了许多其他问题)。

However, because this check is done in the <head>, it is causing the load event to fire, and sometimes the page hasn't fully loaded yet (which is causing lots of other issues).

我试图在第一次加载时覆盖屏幕对象:

I tried overriding the screen object at the first load:

tab.onLoadStarted = function () {
    tab.evaluate(function () {
        window.screen = {
                width: 1920,
                height: 1080
            };
    });
};

但是,该属性是只读的,因此设置它不会改变值。

However, that property is read-only, so setting it does nothing to change the value.

在PhantomJS中,有没有办法覆盖 window.screen 设置我自己的屏幕尺寸的值?

In PhantomJS, is there a way to override the window.screen value to set my own screen size?

推荐答案

你可以'在 LoadStarted 事件中设置 window.screen 值,但您可以在初始化事件。所以,相反,代码看起来像这样:

You can't set the window.screen values in the LoadStarted event, but you can in the Intialized event. So, instead, the code would look like this:

tab.onInitialized = function () {
    tab.evaluate(function () {
        window.screen = {
                width: 1920,
                height: 1080
            };
    });
};

这篇关于如何覆盖window.screen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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