This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
59 lines (55 loc) · 2.15 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
57
58
59
console.log('background running');
//
// chrome.pageAction.onClicked.addListener(clickme);
//
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
// That fires when a page's URL contains a 'g' ...
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlContains: '.pdf' },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.call == "getPrevTitle") {
chrome.history.getVisits({
url: message.url
},
function (arrayVisitItems) {
var currPageTime; // track from visited time of pdf
for (var i = arrayVisitItems.length-1; i >= 0; i--) {
var visitItem = arrayVisitItems[i];
if (visitItem.transition == "link") { // visit must be from another link
currPageTime = visitItem.visitTime;
break;
}
}
// get last 10 visited pages, check if referrer matches url
chrome.history.search({
text: "",
endTime: currPageTime,
maxResults: 10
},
function (arrayHistoryItems) {
var prevDomain = document.referrer;
for (var i = 0; i < arrayHistoryItems.length; i++) {
var historyItem = arrayHistoryItems[i];
if (historyItem.url.search(prevDomain) == 0) {
sendResponse(historyItem.title);
}
}
})
});
}
return true; // very important for asynchronous requests
});