2016-01-15 21 views
0

進樣JS功能我有一個顯示瀏覽器當前工作區一個非常簡單的HTML代碼:與Greasemonkey的

<html> 
<head> 
</head> 
<body> 
<script type="text/javascript"> 
wwidth = document.body.clientWidth; 
wheight = document.body.clientHeight; 
alert("Working area is: " + wwidth + "x" + wheight); 
</script> 
</body> 
</html> 

我嘗試使用以下的Greasemonkey腳本我自己的那些篡改這些值(我已經使用了以下topic作爲參考),這似乎不工作:

// ==UserScript== 
// @name  Resolution 
// @namespace One 
// @include  * 
// @version  1 
// @grant  none 
// ==/UserScript== 

function main() { 
Object.defineProperty(body, "clientWidth", {value: '1000'}); 
Object.defineProperty(body, "clientHeight", {value: '1000'}); 
} 

var script = document.createElement('script'); 
script.appendChild(document.createTextNode('('+ main +')();')); 
(document.body || document.head ||document.documentElement).appendChild(script); 

這是我使用javascript的第一部作品,有些可能會覺得這是一個很簡單的問題,但我根本無法找到一個解決的辦法,因此任何幫助會真的很讚賞編輯。

預先感謝您!

+1

如果你想要做的是重置瀏覽器的高度和寬度,以及我不認爲你可以做到這一點使用JavaScript的。 – sent1nel

+0

我認爲這些值是隻讀的;他們被瀏覽器更新。 – Carcigenicate

回答

0

您可以使用基本的JavaScript這一點,見例如https://jsfiddle.net/5os5r8e2/1/

<body> 
    <input type="button" value="change height" onclick="cHeight()"> 
    <input type="button" value="change width" onclick="cWidth()"> 
</body> 
<script> 
    wwidth = document.body.clientWidth; 
    wheight = document.body.clientHeight; 
    alert("Working area is: " + wwidth + "x" + wheight); 

    function cHeight() { 
    document.body.style.height = "1000px"; 
    } 

    function cWidth() { 
    document.body.style.width = "50%"; 
    } 

</script>