-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
81 lines (72 loc) · 2.49 KB
/
inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//window.addEventListener("load", run);
var idMapping = {}; //maps tweet id to element id.
run();
function run()
{
if($('#claw-report-id-5')[0] == null)
{
idMapping = {};
console.log('Injecting Malicious Buttons in page.');
setTimeout(injectMaliciousButton, 3000);
//listen for messages from background
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
console.log('Inject.js received msg from BG');
console.log(request);
if(request.type == 'malicious-ids-result')
{
console.log('Malicious tweets: ' + request.ids);
markMaliciousTweets(request.ids); //mark mal tweets in red color
}
});
}
}
function markMaliciousTweets(idString)
{
var ids = idString.split(',');
console.log(idMapping);
console.log(ids);
var i;
for(i=0; i<ids.length; i++){
console.log(idMapping[ids[i]]);
$('#claw-tweet-id-'+idMapping[ids[i]]).css('color', 'red');
$('#claw-report-id-'+idMapping[ids[i]]).find('button').css('color', 'red');
}
}
function sendToBackground(data){
chrome.runtime.sendMessage(data);
}
function reportTweet(event){
// get tweeet associated with click event of malicious button
console.log(event.srcElement);
$(event.srcElement).css('color', '#d35400');
var clickID = $(event.srcElement).parent().parent().parent()
.parent().parent().parent()[0].getAttribute('data-item-id');
console.log(clickID);
sendToBackground({type:'report-tweet', tweetID:clickID});
}
function injectMaliciousButton(){
console.log('Injecting buttons');
//inject malicious tweet indicator in every tweet
var maliciousButton = '<button class="ProfileTweet-actionButton u-textUserColorHover" type="button">☹</button>';
var tweets = $('#stream-items-id').find('li.stream-item');
var i=0;
var len = tweets.length;
var ids = '';
while(i<len)
{
//console.log(tweets[i].getAttribute('data-item-id'));
id = tweets[i].getAttribute('data-item-id');
idMapping[id] = i; // id of tweet maps to claw-id
ids += id;
if(i!=len-1)
ids+=','
tweets[i].setAttribute('id', 'claw-tweet-id-'+i.toString()); //insert claw id in element for easy access later
$(tweets[i]).find('div.stream-item-footer').find('div.js-actions')
.append('<div class="ProfileTweet-action">'+maliciousButton+'</div>').attr('id', 'claw-report-id-'+i.toString()).click(reportTweet);
i++;
}
console.log(ids);
//send tweet ids to backend to check which ones are malicious
//this must happen through background.js
sendToBackground({'type':'get-malicious', 'ids':ids});
}