如何在Zendesk Chat iframe中修改SVG图标及引入外部资源?
First off, kudos for already figuring out how to tweak the widget's text— that's half the battle! Let's tackle your SVG modification question and address the Bootstrap/Font Awesome concern, keeping Zendesk's security constraints in mind.
Can I modify the SVG icon with jQuery/JavaScript?
Absolutely! Since you're already successfully accessing the iframe's content via the $zopim callback, you can target the SVG element directly and either update its contents or replace the entire SVG tag.
Here's an example of how to replace the existing chat icon with a custom SVG (like an envelope for a "Contact Sales" context):
$(document).ready(function(){ zE(function(){ $zopim(function(){ // Your existing text modification code $("#launcher").css('width', 'auto'); $("#launcher").contents().find("span.src-component-Launcher-label").text('Contact Sales'); // Replace the SVG icon const targetSvg = $("#launcher").contents().find(".src-component-Launcher-icon svg"); // Option 1: Update the SVG's inner paths (replace with your custom SVG code) targetSvg.html(` <path d="M2.003 5.884L10 9.882l7.997-3.998A2 2 0 0016 4H4a2 2 0 00-1.997 1.884z"></path> <path d="M18 8.118l-8 4-8-4V14a2 2 0 002 2h12a2 2 0 002-2V8.118z"></path> `); // Option 2: Replace the entire SVG element (if you need custom dimensions/attributes) // targetSvg.replaceWith(` // <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20"> // <path d="M2.003 5.884L10 9.882l7.997-3.998A2 2 0 0016 4H4a2 2 0 00-1.997 1.884z"></path> // <path d="M18 8.118l-8 4-8-4V14a2 2 0 002 2h12a2 2 0 002-2V8.118z"></path> // </svg> // `); }); }); });
Just make sure to keep the xmlns attribute on the SVG tag if you go with Option 2— this ensures the SVG renders correctly.
Can I inject Bootstrap/Font Awesome into the iframe?
Unfortunately, this is not feasible due to Zendesk's security restrictions:
- Cross-origin policy: The Zendesk Chat iframe runs on Zendesk's domain, which is different from your website. Browsers block cross-origin DOM manipulation that attempts to load external resources (like CDN-hosted CSS/JS) to prevent security risks.
- Content Security Policy (CSP): Zendesk enforces strict CSP rules on their widgets, which prohibit loading external scripts or stylesheets from unapproved domains (including Bootstrap/Font Awesome CDNs).
Alternative for Font Awesome icons
Instead of loading the entire Font Awesome library, you can:
- Find the Font Awesome icon you want.
- Copy its raw SVG code (either the full
<svg>element or just the<path>tags). - Use that code to replace the existing SVG in the widget, like in the example above. This lets you use Font Awesome icons without needing to inject the entire library.
Key Notes
- Keep an eye on Zendesk's widget updates: If they change their component class names or DOM structure, your selectors might break. Test your code periodically after Zendesk updates.
- Ensure your code runs after the widget is fully loaded: Your current use of
zE()and$zopim()callbacks is the right approach— these ensure the widget's DOM is ready before you modify it.
内容的提问来源于stack exchange,提问作者Aljay

