Make add_invidious_video rule applicable for different invidious instances

This commit is contained in:
alex 2020-09-06 22:41:42 +02:00 committed by GitHub
parent bf2f7d1e16
commit 0f258fd55b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View File

@ -16,7 +16,7 @@ import (
var ( var (
youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`) youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
invidioRegex = regexp.MustCompile(`invidio\.us\/watch\?v=(.*)`) invidioRegex = regexp.MustCompile(`https?:\/\/(.*)\/watch\?v=(.*)`)
imgRegex = regexp.MustCompile(`<img [^>]+>`) imgRegex = regexp.MustCompile(`<img [^>]+>`)
textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`) textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
) )
@ -161,9 +161,8 @@ func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
func addInvidiousVideo(entryURL, entryContent string) string { func addInvidiousVideo(entryURL, entryContent string) string {
matches := invidioRegex.FindStringSubmatch(entryURL) matches := invidioRegex.FindStringSubmatch(entryURL)
fmt.Println(matches) if len(matches) == 3 {
if len(matches) == 2 { video := `<iframe width="650" height="350" frameborder="0" src="https://` + matches[1] + `/embed/` + matches[2] + `" allowfullscreen></iframe>`
video := `<iframe width="650" height="350" frameborder="0" src="https://invidio.us/embed/` + matches[1] + `" allowfullscreen></iframe>`
return video + `<br>` + entryContent return video + `<br>` + entryContent
} }
return entryContent return entryContent

View File

@ -100,7 +100,7 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([
if isExternalResourceAttribute(attribute.Key) { if isExternalResourceAttribute(attribute.Key) {
if tagName == "iframe" { if tagName == "iframe" {
if isValidIframeSource(attribute.Val) { if isValidIframeSource(baseURL, attribute.Val) {
value = rewriteIframeURL(attribute.Val) value = rewriteIframeURL(attribute.Val)
} else { } else {
continue continue
@ -290,7 +290,7 @@ func isBlacklistedResource(src string) bool {
return false return false
} }
func isValidIframeSource(src string) bool { func isValidIframeSource(baseURL, src string) bool {
whitelist := []string{ whitelist := []string{
"https://invidio.us", "https://invidio.us",
"//www.youtube.com", "//www.youtube.com",
@ -312,6 +312,11 @@ func isValidIframeSource(src string) bool {
"https://cdn.embedly.com", "https://cdn.embedly.com",
} }
// allow iframe from same origin
if url.Domain(baseURL) == url.Domain(src) {
return true
}
for _, prefix := range whitelist { for _, prefix := range whitelist {
if strings.HasPrefix(src, prefix) { if strings.HasPrefix(src, prefix) {
return true return true

View File

@ -106,7 +106,7 @@ func TestInvalidNestedTag(t *testing.T) {
func TestInvalidIFrame(t *testing.T) { func TestInvalidIFrame(t *testing.T) {
input := `<iframe src="http://example.org/"></iframe>` input := `<iframe src="http://example.org/"></iframe>`
expected := `` expected := ``
output := Sanitize("http://example.org/", input) output := Sanitize("http://example.com/", input)
if expected != output { if expected != output {
t.Errorf(`Wrong output: "%s" != "%s"`, expected, output) t.Errorf(`Wrong output: "%s" != "%s"`, expected, output)