How to change the script element content using kuchiki?

I want to modify the content of script element if it doesn't refer the other js file.
This is my code.

  if let Some(element) = script_element.as_element() {
      let mut attributes = element.attributes.borrow_mut(); // Borrow the attributes

      // Look up the `src` attribute
      if let Some(src) = attributes.get("src").map(|attr| attr.to_string()) {
          if !server_domain.is_empty() {
              let absolute_src = to_absolute_url(&src, mcop_href, &base_url);
              let modified_src = modify_url(&absolute_src, &server_domain);
              attributes.insert("src", modified_src);
          }
      } else {
          let old_content = script.as_node().to_string();
          let new_content = js_replacer::replace_js_code(&old_content);
  
          // Modify the script's text content
          if let Some(text_node) = script_element.as_text() {
              text_node.borrow_mut().clear();
              text_node.borrow_mut().push_str(&new_content);
          }
      }
  }

In this code, there's a borrow error when I try to get the content of element.
This is the error content.

thread 'tokio-runtime-worker' panicked at C:\Users\USER\.cargo\registry\src\index.crates.io-6f17d22bba15001f\kuchiki-0.8.1\src\serializer.rs:20:52:
already mutably borrowed: BorrowError

How can I fix this error?

It would help to get a backtrace, to see what line the panic is coming from. I'm not sure how to do that on Windows, but the full error message should have a note about how to do it.

But here's a guess. I suspect that when you replace the script_element's contents (via text_node.borrow_mut()...) this is borrowing the node's attributes, which are already borrowed. So you could try dropping attributes before that:

          drop(attributes);  // <<<<<<<<<<

          // Modify the script's text content
          if let Some(text_node) = script_element.as_text() {