-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
130 lines (107 loc) · 2.85 KB
/
background.js
File metadata and controls
130 lines (107 loc) · 2.85 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
importScripts("EventEmitter.js");
importScripts("browser.js");
importScripts("lgtv.js");
var openUrl = function(url)
{
open_browser_at(url, function(err, msg){
console.log(err, msg);
});
};
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
"id": "open_link_to_tv",
"type" : "normal",
"title": chrome.i18n.getMessage("open_link_on_tv"),
"contexts": ["all"]
});
});
var sendLinkToTV = function(e) {
url = e.pageUrl;
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
url = tabs[0].url;
});
if (e.mediaType === "image") {
url = encodeURI(e.srcUrl);
}
if (e.linkUrl) {
// The user wants to buzz a link.
url = e.linkUrl;
}
console.log("sendLinkToTV: ", url);
if (!isConnected)
{
getSetting("options", function(options) {
connect(options.deviceIp, function() { openUrl(url);});
});
}
else
{
openUrl(url);
}
}
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "open_link_to_tv") {
//chrome.runtime.sendMessage({data: info});
sendLinkToTV(info);
}
});
function update()
{
console.log("update");
if (!isConnected)
{
console.log("connecting...");
getSetting("options", function(options) {
connect(options.deviceIp, get_status(function (res, msg) { lightPowerIcon(res); }));
});
}
else
{
get_status(function (res, msg) { lightPowerIcon(res); });
}
}
chrome.runtime.onStartup.addListener(function() {
update();
chrome.alarms.create({periodInMinutes: 1});
}
);
chrome.alarms.onAlarm.addListener(update);
/*
// google analytics
var _AnalyticsCode = "UA-90494817-2";
const ANALYTICS_PATH = "https://www.google-analytics.com/collect";
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST',
mode: 'no-cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: data
});
}
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("background: onMessage", message);
sendResponse("ok");
data = message;
/*
var gaParams = new URLSearchParams();
cid = 555; // FIXME
gaParams.append("v", 1);
gaParams.append("tid", _AnalyticsCode);
gaParams.append("cid", cid);
gaParams.append("ec", "UX");
gaParams.append("t", "pageview");
gaParams.append("dp", data.page);
gaParams.append("t", data.id);
gaParams.append("pa", data.event);
postData(ANALYTICS_PATH, gaParams);
*/
}
);