我有一個Google Apps腳本來獲取網頁並將其輸出。我需要重寫頁面中的任何鏈接以通過腳本加載。如何爲HTML頁面中的每個鏈接添加一個字符串?
即:
<a href="http://stackoverflow">Stackoverflow</a>
將被改寫爲
<a href="https://script.google.com/{snip}exec?http://stackoverflow.com">Stackoverflow</a>
哪裏https://script.google.com/{snip}exec?
是腳本本身的URL。
這是我到目前爲止有:
function getPage(url) {
var options = {
headers : {'Cache-Control' : 'max-age=0'}
};
var response = UrlFetchApp.fetch(url, options);
var html = response.getContentText();
// Prepend this string to each link in the page
var script_url = ScriptApp.getService().getUrl() + "?";
// MAGIC GOES HERE
return HtmlService.createHtmlOutput(html);
}
如何預先考慮script_url
字符串在HTML頁面中的每一個環節?
更新:雖然繼續嘗試自己弄清楚,但我注意到有些頁面在其鏈接中使用相對URL。一個答案需要考慮這一點,並在提供腳本URL之前將相對URL修改爲絕對URL。
(注:我已標記的問題與Javascript
但答案必須是在谷歌企業應用套件腳本可用)