-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
56 lines (46 loc) · 1.3 KB
/
background.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
browser.runtime.onMessage.addListener(function respondToMessage (request, sender, sendResponse){
getCurrentTabUrl(function(url) {
getBechdelResults(getIMDbID(url), function sendRating (response){
sendResponse(response);
});
});
//make sure messaging is asyncyronous
return true;
});
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
browser.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
}
//Make XML HTTP Request to bechdeltest.com to find the specific movie that is being requested
function getBechdelResults(id, callback){
var url = 'http://bechdeltest.com/api/v1/getMovieByImdbId?imdbid=' + id;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(this.readyState == 4){
var response = JSON.parse(this.responseText);
callback(response);
}
};
xhr.open("GET", url, true);
xhr.send();
}
//parse URL and extract IMDb ID
function getIMDbID(url){
var regex = /t{2}\d{7}/; //tt followed by 7 digits
if(url.includes('title')){
var id = url.match(regex)[0];
var scrubID = id.slice(2);
}
else{
scrubID = 0;
}
return scrubID;
}