I'm building a script in Greasemonkey that changes the select tag when clicking on a button. However, the current select has already a onChange option that changes the subcategory.

Is there a way to make the script run this?

HTML Code:

<select id="incident.category" name="incident.category" style=";width:160px;width:180px" onchange="onChange('incident.category');"><option value="">-- None --</option><option value="request" selected="SELECTED">Request</option><option value="incident">Incident</option><option value="informational">Informational</option></select>

My code:

document.getElementById("incident.category").value = "informational";
document.getElementById("incident.category").focus();
window.setTimeout(function ()
{
  document.getElementById("incident.subcategory").value = "informational-informational";
  document.getElementById("incident.subcategory").focus();
}, 1000);

the subcategory doesn't appear if the category doesn't fire the onChange which is what is happening right now.

Any ideas?

有帮助吗?

解决方案

The location hack is a safe and easy way to execute a function defined in the page:

location.assign("javascript:window.onChange('incident.category');void(0)");

It's a lot like running a bookmarklet from within a Greasemonkey script, so it's perfect for scripts that need to a reference only a few things from the page. And, because javascript: URLs always execute in the content scope, this method does not have any of the security concerns that come with using unsafeWindow.

其他提示

Since this is for Greasemonkey and/or Tampermonkey, you can use unsafeWindow. EG:

document.getElementById("incident.category").value = "informational";
document.getElementById("incident.category").focus();
unsafeWindow.onChange('incident.category');
...


Otherwise, the go-to approach is to inject your script code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top