forked from daviddengcn/patch-ext
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
83 lines (75 loc) · 1.77 KB
/
background.js
File metadata and controls
83 lines (75 loc) · 1.77 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
var g_patterns = []
function load() {
var str = localStorage['url_patterns']
if (!str) {
str = [
"https://issues[.]apache[.]org/jira/secure/attachment/[^/]*/.*[.]patch",
"https://issues[.]apache[.]org/jira/secure/attachment/[^/]*/.*[.]txt",
"https://github.com/[^/]*/[^/]*/commit/[^/]*[.]patch",
"https://github.com/[^/]*/[^/]*/commit/[^/]*[.]diff",
"https://gitlab.com/[^/]*/[^/]*/commit/[^/]*[.]patch",
"https://gitlab.com/[^/]*/[^/]*/commit/[^/]*[.]diff",
"https://patch-diff.githubusercontent.com/raw/.*[.]patch",
"https://patch-diff.githubusercontent.com/raw/.*[.]diff"
].join("\n")
}
g_patterns = str.split("\n")
}
load()
function save() {
localStorage['url_patterns'] = g_patterns.join("\n")
}
var shouldInject = function(url) {
for (var i = 0; i < g_patterns.length; i++) {
if (url.match(g_patterns[i])) {
return true
}
}
return false
}
chrome.runtime.onMessage.addListener(
function(msg, sender, sendResponse) {
if (msg.set_options) {
g_patterns = msg.options.patterns
save()
return
}
if (msg.get_options) {
sendResponse({
patterns: g_patterns
})
return
}
}
)
var doRender = function(tabId) {
chrome.tabs.executeScript(tabId, {
file: "cs.js"
})
chrome.tabs.insertCSS(tabId, {
file: "cs.css"
})
}
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
if (changeInfo.status == "complete") {
if (!shouldInject(tab.url)) {
return
}
doRender(tabId)
}
}
)
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {
code: "typeof(g_gpv_rendered) == 'undefined'"
}, function(res) {
if (res[0]) {
doRender(tab.id)
} else {
chrome.tabs.executeScript(tab.id, {
code: "document.body.classList.toggle('gvcrendered')"
})
}
})
})