-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sanitize twitch video embed url (eg : &autoplay=false)
- Loading branch information
vico - low web
committed
Feb 3, 2020
1 parent
acbaebe
commit 441dce7
Showing
4 changed files
with
53 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,54 @@ | ||
import { TOKEN } from '../../datas/constants'; | ||
import { dataTextLink } from '../../utils/data-uri'; | ||
import { watchFilter } from '../../store/watch'; | ||
import videoToBlock from '../../datas/video-to-block'; | ||
import sanitizeEmbedUrl from '../../utils/sanitize-embed-video-url'; | ||
|
||
export function blockEmbedVideo() { | ||
const action = details => { | ||
let response = {}; | ||
if (details.type === 'sub_frame') { | ||
const { url, tabId } = details; | ||
if (url.indexOf('lowweb=' + TOKEN) === -1) { | ||
browser.tabs.sendMessage(tabId, { | ||
message: 'embedVideoBlocked', | ||
url: url, | ||
}); | ||
response.redirectUrl = dataTextLink(url); | ||
// response.cancel = true; | ||
|
||
const { url, tabId } = details; | ||
|
||
// test if url is whitelisted by lowweb TOKEN | ||
if (url.indexOf('lowweb=' + TOKEN) === -1) { | ||
// find video blocked | ||
for (const key of keys) { | ||
if (url.indexOf(videoToBlock[key].embed_url) !== -1) { | ||
if (videoToBlock[key].oembed) { | ||
// send a message to content_script form embed customisation | ||
browser.tabs.sendMessage(tabId, { | ||
message: 'embedVideoBlocked', | ||
url: url, | ||
}); | ||
// redirect to simple fallback just a link to original embed url | ||
response.redirectUrl = dataTextLink(url); | ||
} else { | ||
const sanitizedUrl = sanitizeEmbedUrl(url, false, true); | ||
console.log(sanitizedUrl); | ||
|
||
if (sanitizedUrl !== url) { | ||
response.redirectUrl = sanitizedUrl; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
const embedUrls = []; | ||
const keys = Object.keys(videoToBlock); | ||
for (const key of keys) { | ||
if (videoToBlock[key].embed_url_filter) { | ||
embedUrls.push(videoToBlock[key].embed_url_filter); | ||
} | ||
} | ||
const filter = { | ||
urls: ['*://*.youtube.com/embed/*', '*://player.vimeo.com/*', '*://*.dailymotion.com/embed/*', '*://*.facebook.com/plugins/video.php*'], | ||
urls: embedUrls, | ||
types: ['sub_frame'], | ||
}; | ||
// TODO use a ABP list | ||
// Blocker.filterRequest(action, filter); | ||
|
||
watchFilter('video_clicktoload', action, filter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters