内网环境图片解锁小插件
【摘要】 背景说明:前面有个博客提到:https://bbs.huaweicloud.com/blogs/1736e75674b111eab759fa163e330718这几天就把这个想法实现了一下,测试的目标帖子是:https://zhuanlan.zhihu.com/p/49468787,由于zhuanlan.zhihu.com也不在白名单,所以访问其快照地址https://webcache.go...
背景说明:
前面有个博客提到:
https://bbs.huaweicloud.com/blogs/1736e75674b111eab759fa163e330718
这几天就把这个想法实现了一下,
测试的目标帖子是:https://zhuanlan.zhihu.com/p/49468787,
由于zhuanlan.zhihu.com也不在白名单,所以访问其快照地址
然后可以发现形如"*://*.zhimg.com/*"的图片域也是无法访问的,
开启插件和转发服务前
开启插件和转发服务后
chrome浏览器插件部分
background.js
chrome.webRequest.onBeforeRequest.addListener( function(details) { return { redirectUrl: "http://127.0.0.1:8099/search?q=" + details.url + "&tbm=isch" }; }, { urls: [ "*://*.zhimg.com/*", ], types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"] }, ["blocking"] );
content.js
'use strict'; (function() { var tags = document.getElementsByTagName('noscript'); for (var i = tags.length-1; i >=0 ; i--) { if (tags[i].firstChild) { var newDiv = document.createElement('div'); newDiv.innerHTML = tags[i].innerHTML; tags[i].parentNode.removeChild(tags[i].nextSibling); tags[i].parentNode.replaceChild(newDiv, tags[i]); } } })();
manifest.json
... "background": { "scripts": [ "background.js" ] }, "content_scripts": [{ "matches": ["*://webcache.googleusercontent.com/*"], "js": ["content.js"], "run_at": "document_end" }], "permissions": [ "<all_urls>", "webRequest", "webNavigation", "webRequestBlocking", "browsingData" ] ...
请求转发部分
package main import ( "fmt" "github.com/gorilla/mux" "io" "io/ioutil" "log" "net/http" "os" "regexp" "strings" ) func get_google_img(res http.ResponseWriter,query string) { url1 := "https://www.google.com/search?q=" + query + "&tbm=isch" os.Setenv("HTTP_PROXY", "http://uid:pass@proxy.domain.com:port") resq, err := http.NewRequest("GET", url1, nil) resq.Header.Set("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36") client := &http.Client{} resp, err := client.Do(resq) if err != nil { log.Fatalln(err) } defer resp.Body.Close() if resp.StatusCode != 200 { log.Fatalf("status code error: %d %s", resp.StatusCode, resp.Status) } body,err := ioutil.ReadAll(resp.Body) if err !=nil { log.Fatalln(err) } r := regexp.MustCompile("(https://encrypted-tbn0.gstatic.com/[^\"]+)") matches := r.FindAllStringSubmatch(string(body), -1) decoded := strings.ReplaceAll(matches[0][1],"\\u003d","=") decoded = strings.ReplaceAll(decoded,"\\u0026","&") resq, err = http.NewRequest("GET", decoded, nil) reqImg, err := client.Do(resq) if err != nil { log.Fatalln(err) } if reqImg.StatusCode != 200 { log.Fatalf("status code error: %d %s", reqImg.StatusCode, reqImg.Status) } res.Header().Set("Content-Length", fmt.Sprint(reqImg.ContentLength)) res.Header().Set("Content-Type", reqImg.Header.Get("Content-Type")) if _, err = io.Copy(res, reqImg.Body); err != nil { log.Fatalln(err) } reqImg.Body.Close() } func main() { gmux := mux.NewRouter().StrictSlash(true) gmux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type,Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, X-Auth-Token, content-type") w.Header().Set("content-type", "application/json") w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, POST, DELETE, OPTIONS") q := r.URL.Query().Get("q") get_google_img(w,q) }).Methods("GET") //go func() { http.ListenAndServe(":8099", gmux) //}() }
小结
原来考虑采用chrome.webRequest.replaceStringOccurred,那样适用范围会大些不用一个个适配,但是这个阶段redirectUrl已经不管用了,所以就改成现在的实现;
此外chrome.webRequest.onBeforeRequest的回调也不方便返回异步,所以在background中fetch的方案也有问题;
直接在content.js中fetch需要插件处理跨域问题,综合来说现在的方案 写法还是要简单的多;
----只在内网有些用,外网几本没大用.
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)