문제

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