-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
120 lines (89 loc) · 2.37 KB
/
background.js
File metadata and controls
120 lines (89 loc) · 2.37 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
'use strict';
var menuFact;
var popUpManager;
chrome.runtime.onInstalled.addListener(function () {
initBackGroundProcess()
});
/***
* init watch me plugin engine
*
*/
function initBackGroundProcess() {
var contextManager = new ContextManager();
contextManager.setUpContextMenus();
}
function ContextManager() {
this.selectionText = ''
/**
*
* func which helps to create context menu
*
*/
this.setUpContextMenus = function () {
try {
menuFact = new ContentMenuFactory();
menuFact.create(ADD_WIDGET_COMMAND, ADD_WIDGET);
menuFact.create(REMOVE_WIDGET_COMMAND, REMOVE_WIDGET);
var eventHandler = new EventHandler();
eventHandler.addClickListener();
eventHandler.addBrowserAction();
}
catch (err) {
chrome.tabs.sendMessage(tab.id, { text: 'runtimeExceptionBackGround' }, doStuffWithDom);
}
}
/**
* get selected text from the context
*
*/
this.getSelectedTextFromContext = function () {
return this.selectionText;
}
/**
* set selected text from the context
*
*/
this.setSelectedTextFromContext = function (selectionText) {
this.selectionText = selectionText;
}
}
function EventHandler() {
/****
* method to add click listener to chrome context menu
*
*/
this.addClickListener = function () {
chrome.contextMenus.onClicked.addListener(function (info, tab) {
switch (info.menuItemId) {
case ADD_WIDGET:
chrome.tabs.sendMessage(tab.id, { text: 'addItem' }, callBackAfterOperation);
break;
case REMOVE_WIDGET:
chrome.tabs.sendMessage(tab.id, { text: 'removeItem' }, callBackAfterOperation);
break;
default:
console.log(NO_MATCH_FOUND)
}
});
},
/**
* method to add browser action
*
*/
this.addBrowserAction = function () {
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.create({ url: chrome.extension.getURL('index.html') });
});
}
}
// A function to use as callback
function callBackAfterOperation(data) {
try {
var inputData = JSON.parse(data)
if (inputData.operation == 1) {
addDataToStorage(inputData);
}
} catch (err) {
throw 500;
}
}