-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackground.js
73 lines (63 loc) · 1.71 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
'use strict';
const APPLICABLE_PROTOCOLS = ["http:", "https:"];
function protocolIsApplicable(url) {
var anchor = document.createElement('a');
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}
function showPageActionOnTab(tabInfo){
if (protocolIsApplicable(tabInfo.url)) {
browser.pageAction.show(tabInfo.id);
}
}
function translateCurrentPage() {
browser.tabs.query({
currentWindow: true,
active: true
}, function (foundTabs) {
var currentTabId = foundTabs[0].id;
var executing = browser.tabs.executeScript(currentTabId, {
file: 'scripts/inject_google_translate_content.js'
});
});
}
if (browser.commands) {
browser.commands.onCommand.addListener(function(action) {
if (action == "translate-current-page") {
translateCurrentPage();
}
});
}
if (browser.contextMenus) {
browser.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "translate-current-page") {
translateCurrentPage();
}
});
browser.contextMenus.create({
id: "translate-current-page",
title: browser.i18n.getMessage("translateCurrentPage"),
contexts: ["all"]
});
}
if (browser.browserAction) {
browser.browserAction.setIcon({
"path": {
"19": "images/icon-19.png",
"38": "images/icon-38.png"
}
});
browser.browserAction.onClicked.addListener(translateCurrentPage);
}
if (browser.pageAction) {
browser.pageAction.onClicked.addListener(translateCurrentPage);
browser.tabs.query({}).then((tabs) => {
var tab;
for (tab of tabs) {
showPageActionOnTab(tab);
}
});
browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
showPageActionOnTab(tab);
});
}