-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_titles.js
85 lines (75 loc) · 2.06 KB
/
page_titles.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
// page titles
chrome.storage.local.get({
titles: []
}, function(stored) {
let length = stored.titles.length
for (i = 0; i < length; i++) {
if (stored.titles[i].url == currentUrl) {
document.title = stored.titles[i].title;
}
}
})
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
// Process on-page context menu message searches
if (request.type == "page_title") {
console.log("page_title triggered");
try {
changePageTitle(request.tabUrl);
} catch {
alert("Unable to change page title.");
sendResponse({response: "Unable to change page title."});
}
sendResponse({response: "Page title updated"});
return true
}
else {
//console.log(String(request.type) + " request type unknown");
}
//return true
});
function changePageTitle(tabUrl) {
// check saved for title
let title = window.prompt("Enter new page title")
let oldTitle = document.title;
document.title = title;
chrome.storage.local.get({
titles: []
}, function(stored) {
let foundTitle
// find title
let length = stored.titles.length
for (i = 0; i < length; i++) {
if (stored.titles[i].url == tabUrl) {
foundTitle = i;
}
}
if (title == "") {
//remove from saved
if (foundTitle == undefined) {
// pass
} else {
//Remove from stored.titles
document.title = stored.titles[foundTitle].oldTitle
stored.titles.splice(foundTitle, 1);
// Now update storage...
}
} else {
if (foundTitle == undefined) {
const id = Date.now();
stored.titles.push({title: title, oldTitle: oldTitle, url: tabUrl, id: id});
// Now update storage...
} else {
stored.titles[foundTitle].title = title;
// Now update storage...
}
}
chrome.storage.local.set({
titles: stored.titles
}, function() {
//console.log('Instances is set to:', object);
successAlert("Title Updated");
});
}
);
}