简体   繁体   English

使用Greasemonkey脚本更改页面上的值

[英]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: 我一直在使用此脚本尝试Greasemonkey:

// ==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. 我对javascript的了解非常有限,因此,希望您能提供详细的答复。

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. 编辑:如果有人想尝试一下,请转到此页面http://jahantv.com/Persian/Iran/Iranian/OnLine/Live/Streaming/TVs/ariana-TV.html并运行脚本,并请注意该站点的ridicoulas设计。

No need to use XPath, this sets the value upon page load: 无需使用XPath,这将在页面加载时设置值:

// ==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 . *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. 我验证了<param>属性已更改,但未尝试播放视频,因为我不会安装所需的插件。
  3. If the page changes via ajax methods (doesn't look like it), that can be accounted for, but is a separate question. 如果页面是通过ajax方法更改的(看起来不像),则可以考虑,但这是一个单独的问题。

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. 尝试//param[@name="fullScreen" and @value="0"]来找到该节点,但是我不确定这是否可行:我主要关心的是启动顺序。 Does the Greasemonkey script run right after the page has loaded and before the player is created? Greasemonkey脚本是否在页面加载之后以及创建播放器之前立即运行? 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. 因此,使用代理修改HTML可能会更好,甚至在浏览器看到HTML之前也可以。 Try Privoxy . 尝试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. 对于UNORDERED_NODE_SNAPSHOT_TYPE ,我也感到不安-听起来像“复制HTML并给我克隆”,这意味着对克隆的任何更改都不会影响浏览器窗口中的DOM。 Use ORDERED_NODE_ITERATOR_TYPE instead or avoid XPath altogether. 请改用ORDERED_NODE_ITERATOR_TYPE或完全避免使用XPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM