我试图使用JavaScript获取浏览器描述,结果很奇怪:
productSub: 20100101 # When I open it with my Mozila FF browser
productSub: 20030107 # when I open it with PyQt4
vendorSub: # nothing in here with my Mozila FF
vendorSub: Apple Computer, Inc. # with PyQt4 ( even though I don't have anything to do with Apple)
这是HTML / JS:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<p id="myp">Hi</p>
<script>
body = document.getElementsByTagName('body')[0];
var nav_div = document.createElement('div');
nav_div.id = 'nav_div';
for (para in navigator) {
var para_p = document.createElement('p');
var context = document.createTextNode(para + ': ' + navigator[para]);
para_p.appendChild(context);
para_p.id = para;
nav_div.appendChild(para_p);
}
body.appendChild(nav_div);
</script>
</body>
</html>
这是Python / PyQt4代码:
#! /usr/bin/env python2.7
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys, signal
class MySettings(QWebPage):
def userAgentForUrl(self, url):
return 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0'
class Opener(QWebView):
def __init__(self):
QWebView.__init__(self)
url = QUrl('/home/john-the-ripper/JavaScript/w.html')
self.setPage(MySettings())
self.load(url)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
opener = Opener()
# press Ctrl + C for exit
if signal.signal(signal.SIGINT, signal.SIG_DFL):
sys.exit(app.exec_())
app.exec_()
正如您所看到的,我已经定义了与我的机器中相同的用户代理:
userAgent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0
所以问题是:
productSub
出现不同,即使我已在userAgentForUrl()
中定义vendorSub
为Apple Computer, Inc.
答案 0 :(得分:3)
可能的解决方法是重新定义navigator
对象。首先,将页面主框架中的javaScriptWindowObjectCleared信号连接到插槽:
connect(myPage->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(tweakNavigatorObject()));
在你的插槽中,执行一些可以完全重新定义对象的脚本(QWebFrame::evaluateJavaScript),例如
window.navigator = {
appCodeName: 'Mozilla',
appName: 'Netscape',
vendor: 'My Company'
}
JFYI,这是我在PhantomJS中使用的技巧,它提供了在页面脚本执行之前操纵全局对象的能力,例如,至manipulate Math.random行为并检测user agent sniffing。
答案 1 :(得分:1)
谁说navigator
对象中的信息完全基于UserAgent
标题?该标头仅用于在检索URL时发送到服务器。是的,navigator
对象上的某些值是从当前标题中获取的,但是,正如您所知,并非所有值都是,也不是它们的意思。
事实上,只有appVersion
和userAgent
字符串来自该标头,而其他所有字符串都反映了本机浏览器。
Qt浏览器基于WebKit,这是一个Apple项目,因此是供应商字符串。您可以在Safari中运行navigator
反射代码时看到:
geolocation: [object Geolocation]
cookieEnabled: true
language: en-us
productSub: 20030107
product: Gecko
appCodeName: Mozilla
mimeTypes: [object MimeTypeArray]
vendorSub:
vendor: Apple Computer, Inc.
platform: MacIntel
appName: Netscape
appVersion: 5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17
userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17
plugins: [object PluginArray]
onLine: true
javaEnabled: function javaEnabled() { [native code] }
getStorageUpdates: function getStorageUpdates() { [native code] }
Safari让我也可以从UserAgent
菜单切换Developer
标题。仅切换 也会更改appVersion
和userAgent
字符串。例如,切换到Firefox 11.0 — Mac
会将仅这两个变量更改为:
appVersion: 5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0
userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0
另一个基于WebKit的浏览器是Chrome,它的navigator
信息非常相似:
geolocation: [object Geolocation]
onLine: true
cookieEnabled: true
vendorSub:
vendor: Google Inc.
productSub: 20030107
product: Gecko
mimeTypes: [object MimeTypeArray]
plugins: [object PluginArray]
platform: MacIntel
userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11
language: en-US
appVersion: 5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11
appName: Netscape
appCodeName: Mozilla
doNotTrack: null
javaEnabled: function javaEnabled() { [native code] }
getStorageUpdates: function getStorageUpdates() { [native code] }
webkitStartActivity: function webkitStartActivity() { [native code] }