Skip to content

Commit

Permalink
Update regex validantion
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoVaquel committed Jan 8, 2020
1 parent 6b89d35 commit 2ce793f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default
"profileIdContainerQuerySelector": "a[title='Perfil']",
"targetAdWord": "Sponsored", // In Spanish the word is Publicidad
"postQuerySelector": "hyperfeed_story_id_",
"postSubtitleQuerySelector": "[id^='fe_edsubtitle']",
"postIdQuerySelector": "[name=ft_ent_identifier]"
"postIdQuerySelector": "[name=ft_ent_identifier]",
"postSubtitleQuerySelector": /([\s\S]*.*[mM].*[eE].*[tT].*[aA]*.*[\s\S]|[fF].*[eE].*[eE].*[dD].*[sS].*[uU].*[bB].*[tT].*[iI].*[tT].*[lL].*[eE]|[sS].*[tT].*[oO].*[rR].*[yY]*.*[\s\S])/
}
}
4 changes: 2 additions & 2 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import storage from "./utils/storage"
import config from '../config/config.js'

function handleMessage(request, sender, sendResponse) {
storage.get("pub_elec_location", m => {
storage.get("plugin_location", m => {
var ad = {
userId: request.userId,
userSelectedLocation: m["pub_elec_location"],
userSelectedLocation: m["plugin_location"],
fbPostId: request.postId,
fbAccountId: request.accountId,
visualizedDate: request.visualizedDate
Expand Down
51 changes: 42 additions & 9 deletions src/scripts/fbAds/observer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ext from "../utils/ext"
import config from '../../config/config.js'
const sha256 = require('crypto-js/sha256')
const hex = require('crypto-js/enc-hex')
const sha256 = require('crypto-js/sha256')
const hex = require('crypto-js/enc-hex')

var container = document.querySelector(config.fbAds.mainContainerQuerySelector)
var profile = document.querySelector(config.fbAds.profileIdContainerQuerySelector)
var userId = sha256(profile.href).toString(hex)
var profile = document.querySelector(config.fbAds.profileIdContainerQuerySelector)
var userId = sha256(profile.href).toString(hex)

function searchForAds(mutationsList) {
mutationsList.forEach(mutation => {
Expand All @@ -14,20 +14,41 @@ function searchForAds(mutationsList) {
}

function checkNode(mutation) {
/*
* Check if node is a post and ad.
*/
if (isPost(mutation) && isAd(mutation)) {
let account = getTargetAdAccount(mutation)
if (account) {
//add blue border to identify target ad
// Add a border to identify target ads.
mutation.target.style.border = "3px solid #49c096"
// Prepare data for will be send to backend.
let adData = buildData(mutation, account)
sendAd(adData)
}
}
}

function testOfRegex(node, regex) {
/*
* Test if node matchs with some regex and return it.
*/
var all_tags = node.getElementsByTagName("*");
var results = [];
for (var i = all_tags.length-1; i >= 0; -- i) {
if (regex.test(all_tags[i].id)) {
results.push(all_tags[i]);
}
}
return results;
}

function getTargetAdAccount(node) {
let subtitleNode = node.target.querySelector(config.fbAds.postSubtitleQuerySelector)
let titleNode = subtitleNode.previousSibling.innerHTML
/*
* Checking if ad is one of accounts to monitoring.
*/
let subtitleNode = testOfRegex(node.target, config.fbAds.postSubtitleQuerySelector)
let titleNode = subtitleNode[0].previousSibling.innerHTML
let account = config.accounts.filter( (account) => {
return titleNode.includes(account.page_id) && titleNode.includes(account.page_name)
})
Expand All @@ -46,15 +67,22 @@ function buildData(node, account) {
}

function isAd(node) {
let subtitleNode = node.target.querySelector(config.fbAds.postSubtitleQuerySelector)
let subtitle = subtitleNode.innerText.replace(/-/g, '')
/*
* Check if the node is an Ads.
*/
let subtitleNode = testOfRegex(node.target, config.fbAds.postSubtitleQuerySelector)
let subtitle = subtitleNode[0].innerText.replace(/-/g, '')
if (subtitle.includes(config.fbAds.targetAdWord)) {
// Put a border to identify the ads.
node.target.style.border = "3px solid #FF00FF"
}
return subtitle.includes(config.fbAds.targetAdWord)
}

function isPost(node) {
/*
* Check if the node is a Post.
*/
return node.type === "childList" && node.target.id.startsWith(config.fbAds.postQuerySelector)
}

Expand All @@ -63,6 +91,11 @@ function sendAd(ad) {
}

function run() {
/*
* Instance a MutationObserver that listens to the HTML nodes for new changes
* when you scroll the Facebook feed for new posts.
* You can gives to MutationObserver a function to execute each time that the HTML changes.
*/
const observer = new MutationObserver(searchForAds)
observer.observe(container, {subtree: true, childList: true})
}
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const locations = config.locations
document.addEventListener("DOMContentLoaded", function(event) {
var locationSelect = document.getElementById("location")

storage.get("pub_elec_location", m => {
storage.get("plugin_location", m => {
for (var i = 0; i < locations.length; i++) {
var opt = document.createElement("option")
opt.setAttribute('value', locations[i]);
opt.appendChild(document.createTextNode(locations[i]));

if (m["pub_elec_location"] === locations[i]) {
if (m["plugin_location"] === locations[i]) {
opt.selected = "selected"
}

Expand All @@ -22,6 +22,6 @@ document.addEventListener("DOMContentLoaded", function(event) {
})

locationSelect.addEventListener("change", function(e) {
storage.set({pub_elec_location: e.target.value})
storage.set({plugin_location: e.target.value})
})
})

0 comments on commit 2ce793f

Please sign in to comment.