├── manifest.json ├── README.md └── contentscript.js /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Simulate Device Hack", 3 | "version": "1.0", 4 | "description": "Modify the navigator.platform while simulating a mobile viewport", 5 | "manifest_version": 2, 6 | "content_scripts": [ 7 | { 8 | "matches": [""], 9 | "js": ["contentscript.js"], 10 | "run_at": "document_start" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ​ You can use **Device Mode** in **Chrome DevTools**(F12) to simulate a mobile viewport. 2 | 3 | But it only change the `navigator.userAgent`, while `navigator.platform` remains unchanged. 4 | 5 | This Chrome extension helps you solve this problem. 6 | 7 | 8 | 9 | ​ 我们在使用Chrome F12模拟手机请求的时候,Chrome只会修改UserAgent等属性,`navigator.platform`保持不变,Windows平台下该值一般为`Win32`。 10 | 11 | ​ 而国内很多网站,是根据`navigator.platform`做跳转,比如某度。该插件就是解决这个问题。 -------------------------------------------------------------------------------- /contentscript.js: -------------------------------------------------------------------------------- 1 | // see https://stackoverflow.com/questions/23202136/changing-navigator-useragent-using-chrome-extension 2 | var actualCode = '(' + function() { 3 | // Get the correct platform for navigator.userAgent 4 | var candidates = ["Android", "iPhone", "iPod", "iPad"]; 5 | var modifiedPlatform; 6 | for(var maybe of candidates) { 7 | if(navigator.userAgent.match(maybe)) { 8 | modifiedPlatform = maybe; 9 | break; 10 | } 11 | } 12 | 13 | if(!modifiedPlatform) { 14 | return; 15 | } 16 | 17 | // Reset platform 18 | Object.defineProperty(navigator, 'platform', { 19 | get: function(){ 20 | return modifiedPlatform; 21 | }}); 22 | } + ')();'; 23 | 24 | // Inject custom js 25 | var s = document.createElement('script'); 26 | s.textContent = actualCode; 27 | document.documentElement.appendChild(s); 28 | s.remove(); --------------------------------------------------------------------------------