Inject JS function with Greasemonkey

I have a pretty simple HTML code that displays browsers current working area:

<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>

I am trying to tamper these values with my own ones using the following Greasemonkey script (I have used the following topic as a reference) which does not seem to work:

// ==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);

This is my first work with javascript and some may find this to be a pretty simple issue but I simply can not find a solution to it so any help would be really appreciated.

Thank you in advance!

Answers


You can use basic javascript for this, see example 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>

Need Your Help

Firefox expands select size when appending content

javascript jquery firefox append

I'm currently working on a script that loads dynamic content (fetched via Ajax) into a select. Since I am getting multiple elements, my code currently looks something like this

Prevent Doctrine migration diff from deleting tables

doctrine doctrine-migrations

I'm currently in the process of "Doctrinizing" an large existing database. This process is working very well and we're slowly able to substitute the old raw query code with entities and DQL.