-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
229 lines (201 loc) · 9.19 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// manifest.json required to restore the popup menu
/*
"browser_action": {
"default_icon": "2.png",
"default_popup": "popup.html"
},
*/
var result1 = "List 1";
var result2 = "List 2";
chrome.storage.sync.get("result1Value", //// load saved data.
function(res1) {
if (res1.result1Value === undefined){
console.log("nothin saved 1");
clf1(result1,result2);
} else {
result1=res1.result1Value;
//$("#list1").html(result1);
clf1(result1,result2);
}
});
chrome.storage.sync.get("result2Value", //// load saved data.
function(res2) {
if (res2.result2Value === undefined){
console.log("nothin saved 2");
clf1(result1,result2);
} else {
result2=res2.result2Value;
//$("#list2").html(result2);
clf1(result1,result2);
}
});
var wordObjectB = [{ word: 'test word', definition:'this word is used for back testing' }];
var wordObjectB2 = [{ word: 'test word 2', definition:'this word is used for back testing 2' }];
var loadFunctionB = function(){
//chrome.storage.sync.set({"myValueB": wordObjectB}); activate this to get the program running. The emply storage will throw an error
chrome.storage.sync.get("myValueB", //// load saved data.
function(valB) {
if (valB.myValueB === undefined){
chrome.storage.sync.set({"myValueB": wordObjectB});
//runArrayB();
} else {
wordObjectB=valB.myValueB;
chrome.storage.sync.set({"myValueB": wordObjectB});///save
}
});
};
loadFunctionB();
var loadFunctionB2 = function(){
//chrome.storage.sync.set({"myValueB": wordObjectB}); activate this to get the program running. The emply storage will throw an error
chrome.storage.sync.get("myValueB2", //// load saved data.
function(valB2) {
if (valB2.myValueB2 === undefined){
chrome.storage.sync.set({"myValueB2": wordObjectB2});
} else {
wordObjectB2=valB2.myValueB2;
chrome.storage.sync.set({"myValueB2": wordObjectB2});///save
}
});
};
loadFunctionB();
var runArrayB = function (){
chrome.storage.sync.set({"myValueB": wordObjectB});///save
chrome.storage.sync.set({"myValueB2": wordObjectB2});///save
};
function openTab(filename)
{
var myid = chrome.i18n.getMessage("@@extension_id");
chrome.windows.getCurrent(
function(win)
{
chrome.tabs.query({'windowId': win.id},
function(tabArray)
{
for(var i in tabArray)
{
if(tabArray[i].url == "chrome-extension://" + myid + "/" + filename)
{
// console.log("already opened");
chrome.tabs.update(tabArray[i].id, {active: true});
return;
}
}
chrome.tabs.create({url:chrome.extension.getURL(filename)});
});
});
}
runArrayB();
var wikiDefineShortB ="word not found - double click here to add definition";
var baseURL = 'http://en.wiktionary.org';
function showPage(page,text) {
var sourceurl = baseURL + '/wiki/' + page;
$('#pagetitle').text(page);
$('#wikiInfoB').html(text);
$('#sourceurl').attr('href',sourceurl);
$('#licenseinfo').show();
$('#wikiInfoB').children("ol:lt(1)").attr('', ////////////////// change number of drfinitions eg verb and noun
function() { //console.log(this);
//this.children("li:nth-child(-n+2)");
$(" ol li ul").detach();
$(" ol li ul").detach();
var wikiDefineB = this.textContent;
wikiDefineShortB = jQuery.trim(wikiDefineB).substring(0, 500) /////// shortening the definition for google local storage
.trim(this) + "...";
//var spellItem = document.getElementById('word');
runArrayB();
// wordObjectB[wordObjectB.length]= { word: page, definition: wikiDefineShortB };
// runArrayB();
// chrome.storage.sync.set({"myValueB": wordObjectB}); /////save
});
}
$(document).ready(function() {
chrome.browserAction.onClicked.addListener(function(activeTab){
var tabs = chrome.extension.getViews({type: "tab"});
var newURL = "index.html";
if(tabs[0]===undefined){
chrome.tabs.create({ url: newURL });
} else {
openTab("index.html");
}
});
$('#pagetitle').hide();
$('#word').change(function() {
var page = this.value;
$('#wikiInfoB').html('...please wait...');
$.getJSON(baseURL+'/w/api.php?action=parse&format=json&prop=text|revid|displaytitle&page='+page,
function(json) {
if(json.parse.revid > 0) {
showPage(page,json.parse.text['*']);
$('#wikiInfoB').html("<div></div>");
} else {
$('#wikiInfoB').html('word not found');
$('#licenseinfo').hide();
}
});
});
});
var clf1 = function (r1,r2){
chrome.contextMenus.removeAll();
chrome.contextMenus.create({title: "Add '%s' to " + r1,
contexts:["selection"],
onclick: function(info){
runArrayB();
console.log(wordObjectB);
var page = info.selectionText.toLowerCase();
$('#wikiInfoB').html('...please wait...');
$.getJSON(baseURL+'/w/api.php?action=parse&format=json&prop=text|revid|displaytitle&page='+page,
function(json) {
console.log(json.parse);
if(json.parse === undefined) {
console.log("word not found");
wordObjectB[wordObjectB.length]= { word: info.selectionText.toLowerCase(), definition: "word not found - double click here to add definition" };
runArrayB();
chrome.storage.sync.set({"myValueB": wordObjectB}); /////save
} else {
showPage(page,json.parse.text['*']);
wordObjectB[wordObjectB.length]= { word: info.selectionText.toLowerCase(), definition: wikiDefineShortB };
$('#wikiInfoB').html("<div></div>");
runArrayB();
chrome.storage.sync.set({"myValueB": wordObjectB}); /////save
}
var tabs = chrome.extension.getViews({type: "tab"});
if (tabs[0]===undefined){
console.log("not on front page");
}else{
tabs[0].loadBackgroundList();
}
});
}
});
chrome.contextMenus.create({title: "Add '%s' to " + r2,
contexts:["selection"],
onclick: function(info){
runArrayB();
console.log(wordObjectB);
var page = info.selectionText.toLowerCase();
$('#wikiInfoB').html('...please wait...');
$.getJSON(baseURL+'/w/api.php?action=parse&format=json&prop=text|revid|displaytitle&page='+page,
function(json) {
console.log(json.parse);
if(json.parse === undefined) {
console.log("word not found");
wordObjectB2[wordObjectB2.length]= { word: info.selectionText.toLowerCase(), definition: "word not found - double click here to add definition" };
runArrayB();
chrome.storage.sync.set({"myValueB2": wordObjectB2}); /////save
} else {
showPage(page,json.parse.text['*']);
wordObjectB2[wordObjectB2.length]= { word: info.selectionText.toLowerCase(), definition: wikiDefineShortB };
$('#wikiInfoB').html("<div></div>");
runArrayB();
chrome.storage.sync.set({"myValueB2": wordObjectB2}); /////save
}
var tabs = chrome.extension.getViews({type: "tab"});
if (tabs[0]===undefined){
console.log("not on front page");
}else{
tabs[0].loadBackgroundList();
}
});
}
});
};