我有一個通過對雲數據庫解決方案的API調用獲取其大部分內容的網頁。 HTML頁面相當準確,但通過一些JS/JQuery命令獲得更多數據注入等。僅從包含整個網頁的變量中刪除腳本標記
生成的頁面代表一個「Quote」,我想將它保存回雲數據庫以供參考。
我可以得到網頁的當前狀態,並通過使用以下命令將其存儲在一個變量:
var AVMI_thisPage = document.getElementsByTagName('html')[0].outerHTML;
我現在需要從變量刪除任何<script>
標記,以便在任何HTML再次導入回到雲端數據庫不包含任何JS,當有人打開它供參考時,它很可能再次混淆頁面。
我應該能夠將字符串推回到數據庫,但我需要擺脫任何<script>
。
我試過JQuery,但這似乎殺死了HTML,HEAD和BODY標籤。
說實話,我沒想到下面的代碼無論如何工作......但嘗試過。
E.g.
var AVMI_thisPage = document.getElementsByTagName('html')[0].outerHTML;
var AVMI_tree = $("<div>" + AVMI_thisPage + "</div>");
AVMI_tree.find('script').remove();
AVMI_thisPage = AVMI_tree.html();
任何想法?
更新 - 最終的代碼(包括BASE64編碼和上傳)
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
var htmlPage = $("html");
$("script", htmlPage).remove();
AVMI_thisPage = htmlPage.html();
AVMI_thisPageB64 = b64EncodeUnicode(AVMI_thisPage);
var req = "";
req += "<qdbapi>";
req += "<rid>" + AVMI_quoteRID + "</rid>";
req += "<field fid='171' filename='Hardcopy of Quote.html'>"+ AVMI_thisPageB64 + "</field>";
req += "</qdbapi>";
$.ajax({
type: "POST",
contentType: "text/xml",
dataType: "xml",
processData: false,
url: "https://xxxx.xxxxxxxx.com/db/" + AVMI_Q_DBID + "?act=API_UploadFile",
data: req
})
.then(function() {
alert("A copy of this quote has been saved into the 'Hardcopy Attachment' field.");
window.close();
});
請問[這個其他問題]的答案(http://stackoverflow.com/questions/8503560/how-do-i-use-jquery-to-remove-all-script-tags-in-a-string -of-html)的工作? – Callum