use of org.pdown.gui.extension.ContentScript in project proxyee-down by monkeyWie.
the class ScriptIntercept method handelResponse.
@Override
public void handelResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
List<ExtensionInfo> extensionInfoList = ExtensionContent.get();
if (isEmpty(extensionInfoList)) {
return;
}
for (ExtensionInfo extensionInfo : extensionInfoList) {
if (isEmpty(extensionInfo.getContentScripts())) {
continue;
}
for (ContentScript contentScript : extensionInfo.getContentScripts()) {
// 扩展注入正则表达式与当前访问的url匹配则注入脚本
String url = HttpDownUtil.getUrl(httpRequest);
if (contentScript.isMatch(url)) {
String apiTemplate = readInsertTemplate(extensionInfo);
StringBuilder scriptsBuilder = new StringBuilder();
for (String script : contentScript.getScripts()) {
File scriptFile = new File(extensionInfo.getMeta().getFullPath() + File.separator + script);
if (scriptFile.exists() && scriptFile.isFile()) {
try {
scriptsBuilder.append(new String(Files.readAllBytes(scriptFile.toPath()), "UTF-8"));
} catch (IOException e) {
}
}
}
apiTemplate = apiTemplate.replace("${content}", scriptsBuilder.toString());
int index = ByteUtil.findText(httpResponse.content(), INSERT_TOKEN);
ByteUtil.insertText(httpResponse.content(), index == -1 ? 0 : index - INSERT_TOKEN.length(), apiTemplate, Charset.forName("UTF-8"));
}
}
}
}
Aggregations