Search in sources :

Example 1 with ContentScript

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"));
            }
        }
    }
}
Also used : ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) ContentScript(org.pdown.gui.extension.ContentScript) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ContentScript (org.pdown.gui.extension.ContentScript)1 ExtensionInfo (org.pdown.gui.extension.ExtensionInfo)1