-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·109 lines (97 loc) · 3.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var note_id;
var yt_id;
var prev_subs = undefined;
var title = undefined;
var url;
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: 'youtube.com' },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if( request.message === "response_subs" )
{
if(prev_subs == undefined && title != undefined)
{
chrome.storage.sync.set({'note': "#"+title+"$\n\n"+request.curr_subs.replace(/("\r\n|\n|\r)/gm, " ")}, function() {});
}
else if(request.curr_subs != prev_subs)
{
chrome.storage.sync.set({'note': " "+request.curr_subs.replace(/("\r\n|\n|\r)/gm, " ")}, function() {});
}
prev_subs = request.curr_subs;
}
else if(request.message === "download" )
{
chrome.tabs.create({"url": 'http://younote.pythonanywhere.com/'});
}
else if(request.message === "toggle_auto" )
{
chrome.tabs.sendMessage(yt_id, {"message": "request_subs"});
}
else if(request.message === "response_title" )
{
title = request.yt_title;
}
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var new_url = changeInfo.url
if(tabId == yt_id && new_url != undefined && new_url != url)
{
setTimeout(function()
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "request_title"});
});
url = new_url;
prev_subs = undefined;
}, 5000);
}
});
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "request_title"});
yt_id = activeTab.id;
});
var maxWidth = window.screen.availWidth;
var maxHeight = window.screen.availHeight;
chrome.windows.getCurrent(function(wind) {
var updateInfo = {
left: 0,
top: 0,
width: parseInt(maxWidth*3/4),
height: maxHeight,
drawAttention:true,
state:"normal"
};
chrome.windows.update(wind.id, updateInfo);});
var value = " Initializing... ";
chrome.storage.sync.set({'note': value}, function() {});
chrome.windows.create({"url": chrome.extension.getURL('notes.html'),
"type":"popup",
"left":parseInt(maxWidth*10/12),
"width":parseInt(maxWidth*1/4),
"height":parseInt(maxHeight)
});
chrome.windows.getLastFocused({"windowTypes":['popup']},function(wind) {
note_id = wind.id;
});
});
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.sendMessage(yt_id, {"message": "request_subs"});
});