[英]Fire “onchange” event on page from google chrome extension
I am developing an extension to autofill a form with data from another website.我正在开发一个扩展程序,用来自另一个网站的数据自动填充表单。 I have a context menu with different options, each with different data but complete the same form.
我有一个包含不同选项的上下文菜单,每个选项都有不同的数据但完成相同的表单。 The data is dynamic and depend on another website.
数据是动态的,取决于另一个网站。 With "chrome.tabs.executeScript" i can insert some data, but i can not fire the event "onchange" of the fields.
使用“chrome.tabs.executeScript”,我可以插入一些数据,但我无法触发字段的“onchange”事件。 Can change the value of a select, but the website has an event "onchange" that does not fire when I change the value, and not get through "chrome.tabs.executeScript" run the "onchange".
可以更改选择的值,但是网站有一个事件“onchange”,当我更改值时不会触发该事件,并且无法通过“chrome.tabs.executeScript”运行“onchange”。 The error it shows is: "Uncaught TypeError: Property 'onchange' of object # is not a function #HTMLSelectElement"
它显示的错误是:“Uncaught TypeError: Property 'onchange' of object # is not a function #HTMLSelectElement”
EDIT:编辑:
manifest.json:清单.json:
"content_scripts": [
{
"matches": ["url_where_the_code_must_run"],
"js": ["billing.class.js", "script.js"]
}
],
.
.
.
"permissions": [
"tabs",
"url_from_where_i_get_the_data",
"url_where_the_code_must_run"
],
script.js:脚本.js:
select = document.createElement('select');
var option = document.createElement('option');
option.value = "";
option.innerHTML = "Select Client";
select.appendChild(option);
select.onchange = function() {
var client = Billing.getClient(this.value);
if (client) {
document.getElementsByName("country")[0].value = client.country_id;
document.getElementsByName("country")[0].onchange();
}
}
for (var i = 0; i < Billing.clients.length; i++) {
option = document.createElement('option');
option.value = Billing.clients[i].id;
option.innerHTML = Billing.clients[i].name;
select.appendChild(option);
}
form.parentElement.insertBefore(select, form);
When this line is executed, show "Uncaught TypeError: Property 'onchange' of object # is not a function #HTMLSelectElement":执行此行时,显示“Uncaught TypeError: Property 'onchange' of object # is not a function #HTMLSelectElement”:
document.getElementsByName("country")[0].onchange();
But if I run this line directly in the console, normally runs without errors但是如果我直接在控制台中运行这一行,通常运行没有错误
document.getElementsByName("country")[0] is an element of the url, and "select" is an element that i create in the extension to select what client data should load. document.getElementsByName("country")[0] 是 url 的一个元素,“select”是我在扩展中创建的一个元素,用于选择应该加载哪些客户端数据。
url_where_the_code_must_run: url_where_the_code_must_run:
<script type="text/javascript">
function customFunction() {
// Do something that i dont care
}
</script>
<select name="country" onchange="customFunction()">
</select>
Have you tried emitting the event rather than directly calling the function?您是否尝试过发出事件而不是直接调用函数? As far as I remember, calling event-handlers directly can have side effects.
据我所知,直接调用事件处理程序会产生副作用。 I can't find a source at the moment though.
不过我暂时找不到消息来源。
Try to replace尝试更换
document.getElementsByName("country")[0].onchange();
with与
var changeEvent = document.createEvent("HTMLEvents");
changeEvent.initEvent("change", true, true);
document.getElementsByName("country")[0].dispatchEvent(changeEvent);
I also created a little fiddle: http://jsfiddle.net/d94Xb/1/我还创建了一个小小提琴: http : //jsfiddle.net/d94Xb/1/
Furthermore you might look into addEventListener()
and dispatchEvent()
a bit more as this is the common way to register and execute event handler.此外,您可能会更多地研究
addEventListener()
和dispatchEvent()
因为这是注册和执行事件处理程序的常用方法。
(update: 2015-04-27) The method described above is deprecated by now as @10basetom points out. (更新:2015-04-27)正如@10basetom 指出的那样,上述方法现在已被弃用。 The better way is to use Event() .
更好的方法是使用Event() 。 Please note that this won't work in IE and Safari.
请注意,这不适用于 IE 和 Safari。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.