简体   繁体   English

将查询参数附加到html字符串中的img src url

[英]append query param to img src url in a html string

 <p style="text-align: center"><img alt="Editor photo" class="fr-fir fr-dii fr-draggable" src="/assets/editor/pages/B/editor-photo-3eeba30c6186df86c859d6d14e9b02d0.jpg" width="200"></p> <p>Froala Editor is a lightweight WYSIWYG HTML Editor written in Javascript that enables rich text editing capabilities for your applications.</p> <p>Its complete <a href="/wysiwyg-editor/docs" title="Documentation">documentation</a>, specially designed <a href="#frameworks" title="Frameworks">framework plugins</a> and <a href="/wysiwyg-editor/examples" title="Examples">tons of examples</a> make it easy to integrate. We&#39;re continuously&nbsp;</p> <p>working to add in new features and take the Javascript web WYSIWYG editing capabilities beyond its current limits.</p> <p> <br> </p> <p><img alt="Editor photo" class="fr-dii fr-draggable fr-fil" src="/assets/editor/pages/B/editor-photo-3eeba30c6186df86c859d6d14e9b02d0.jpg" width="200"></p> <p> <br> </p> <p><span class="fr-video fr-dvi fr-draggable" contenteditable="false" style=""><video controls=""><source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm"><source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg"><source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4"><source src="http://techslides.com/demos/sample-videos/small.3gp" type="video/3gp"></video></span></p> 

I'm looking for script to append query param to all image, video src url in html string and this need to be done before rendering to the DOM. 我正在寻找将查询参数附加到所有图像,html字符串中的视频src url的脚本,并且需要在呈现到DOM之前完成此操作。 Input pattern is similar to what i have shared. 输入模式类似于我分享的内容。 Query param i'm trying to attach is a token which will be used to validate if image can be accessed or not. 我尝试附加的查询参数是一个令牌,将用于验证是否可以访问图像。

I'm using nodejs and angular2 frameworks 我正在使用nodejsangular2框架

Jquery Approach jQuery方法

var wrapHtmlString =  `<div>${htmlstring}</div>`;
var pageHtml = $(wrapHtmlString);

pageHtml.find('[src]').each(function () {
    var rawImageSrc = $(this).attr('src');

    var tokenizedImageSrc = rawImageSrc+'?token=randomtoken';
    $(this).attr('src', tokenizedImageSrc);
});

var formattedHtmlString = pageHtml.html();

You can try the append-query npm package which can meet your requirement. 您可以尝试满足您要求的append-query npm软件包。

You can install it by issuing the below command, 您可以通过发出以下命令来安装它,

npm install append-query

Check the documentation here: https://www.npmjs.com/package/append-query 在此处查看文档: https : //www.npmjs.com/package/append-query

Hope this helps!. 希望这可以帮助!。

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

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