简体   繁体   English

在内容脚本中侦听AJAX响应何时更改文本区域值

[英]Listen in Content Script for when AJAX response changes textarea val

I'm writing a WebExtension for a text editor. 我正在为文本编辑器编写WebExtension When the user clicks a button, format the editor fires an ajax request and then returns the formatted text. 当用户单击按钮时, format编辑器将触发ajax请求,然后返回格式化的文本。 I need my addon to listen for a change in the textarea . 我需要我的插件来收听textarea的更改。

I've tried using onchange or oninput , but when the response is received, these events aren't triggered. 我尝试使用onchangeoninput ,但是当收到响应时, 不会触发这些事件。 The ajax response sets the body with this function (not my own): Ajax响应使用此功能(不是我自己的)设置主体:

function setBody(text) {
  $(opts.codeEl).val(text); // opts.codeEl is the textarea in question
}

the ajax response looks like this: ajax响应如下所示:

{"Body":"Hello World","Error":""}

Is it possible to handle this ajax request/response from a WebExtension Content Script ? 是否可以通过WebExtension Content Script处理此Ajax请求/响应? Somehow listen for a change in val of the textarea in the DOM ? 以某种方式侦听DOM中 textarea val变化? Can I listen and intercept the response? 我可以听和截取响应吗?

The ajax request is being sent and received in the website's javascript code, isolated from the content script which I'm using (the WebExtension code) for the editor. Ajax请求是通过网站的javascript代码发送和接收的, 我正在使用的内容脚本(WebExtension代码) 隔离开来。 the request is this 请求是这个

function fmt() {
  loading();
  var data = {"body": body()};
  if ($(opts.fmtImportEl).is(":checked")) {
    data["imports"] = "true";
  }
  $.ajax("/fmt", {
    data: data,
    type: "POST",
    dataType: "json",
    success: function(data) {
      if (data.Error) {
        setError(data.Error);
      } else {
        setBody(data.Body);
        setError("");
      }
    }
  });
}

Edit 编辑

Injecting a ajaxComplete handler: 注入ajaxComplete处理程序:

  $(document).ajaxComplete(function(event, jqxhr, options) {
    if(options.url === "/fmt") {
      console.log("fmtted");
      $('#code').change();
    }
  });

Into the target site header will be called when the ajax response is received. 当接收到ajax响应时,将调用目标站点标头。 However, when trying to modify the DOM using injected scripts (by, for example, calling change() or onchange() , for security reasons, it raises an Error: Permission denied to access property "apply" error. 但是,当出于安全原因尝试使用注入的脚本修改DOM (例如,通过调用change()onchange() ,将引发Error: Permission denied to access property "apply"错误。

jQuery .val(value) does does dispatch change event. jQuery .val(value)确实调度了change事件。 Call .change() or .trigger("change") . 调用.change().trigger("change")

If you do not know when setBody will be called you can use .ajaxStop() to call .change() on $(opts.codeEl) when last $.ajax() call completes. 如果你不知道什么时候setBody将被调用,您可以使用.ajaxStop()调用.change()$(opts.codeEl)当最后$.ajax()调用完成。

 $(function() { let opts = { codeEl: document.querySelector("textarea") } $(opts.codeEl).on("change", function(event) { console.log("changed") }); function setBody(text) { $(opts.codeEl).val(text) } let url = URL.createObjectURL( new Blob([ JSON.stringify({ "Body": "Hello World", "Error": "" }) ], { type: "application/json" }) ); $.get(url) .then(function(data) { URL.revokeObjectURL(url); setBody(JSON.stringify(data)) }); $(document).ajaxComplete(function(event, jqxhr, options) { if (options.url === url) { console.log("ajaxStop"); $(opts.codeEl).change(); // trigger `change` event } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <textarea></textarea> 

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

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