user516242
user516242

Reputation:

Greasemonkey script to change a value on a page

I want to change this value:

<param name="fullScreen" value="0">

To this:

<param name="fullScreen" value="1">

On a page. I have been experimenting with Greasemonkey with this script:

// ==UserScript==
// @name           Fullscreen
// @namespace      http://localhost
// @include        http://jahantv.com*
// @include        *jahantv*
// ==/UserScript==

var i, x = document.evaluate('//*[@name="fullScreen"][@value="0"]', document,
null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for ( i=0 ; i < x.snapshotLength; i++ )
    x.snapshotItem(i).setAttribute("value", "1"); 

But it is not functioning. There must be a simple way of doing this. My knowledge of javascript is very limited, therefore I would appreciate any reply with details.

EDIT: If anyone want to try it out, go to this page http://jahantv.com/Persian/Iran/Iranian/OnLine/Live/Streaming/TVs/ariana-TV.html and run the script, and kindly mind the ridicoulas design of that site.

Upvotes: 2

Views: 3085

Answers (2)

Brock Adams
Brock Adams

Reputation: 93443

No need to use XPath, this sets the value upon page load:

// ==UserScript==
// @name           Fullscreen
// @namespace      http://localhost*
// @include        http://jahantv.com*
// @include        *jahantv*
// ==/UserScript==

var fullScrParam    = document.querySelector("[name='fullScreen']");
fullScrParam.value  = "1";

Notes:

  1. * was missing after localhost.
  2. I verified that the <param> attribute was changed but did not attempt to play a video as I won't install the required plugin.
  3. If the page changes via ajax methods (doesn't look like it), that can be accounted for, but is a separate question.

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328536

Try //param[@name="fullScreen" and @value="0"] to locate the node but I'm unsure whether this will work: My main concern is the startup order. Does the Greasemonkey script run right after the page has loaded and before the player is created? Will the player code get an update when you change the value after it has been started?

So you might fare better using a proxy which modifies the HTML even before the browser can see it. Try Privoxy.

I'm also uneasy about UNORDERED_NODE_SNAPSHOT_TYPE - that sounds like "copy the HTML and give me the clone" which means that any changes to the clone will have no effect on the DOM in the browser window. Use ORDERED_NODE_ITERATOR_TYPE instead or avoid XPath altogether.

Upvotes: 0

Related Questions