-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
85 lines (73 loc) · 2.4 KB
/
Script.js
File metadata and controls
85 lines (73 loc) · 2.4 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
(function() {
"use strict";
// This following code is taken from
// https://github.com/thieman/github-selfies/blob/master/chrome/selfie.js
var allowedPaths = [
/github.com\/[\w\-]+\/[\w\-]+\/pull\/\d+/
];
// Inject the code from fn into the page, in an IIFE.
function inject(fn) {
var script = document.createElement('script');
var parent = document.documentElement;
script.textContent = '('+ fn +')();';
parent.appendChild(script);
parent.removeChild(script);
}
// Post a message whenever history.pushState is called. GitHub uses
// pushState to implement page transitions without full page loads.
// This needs to be injected because content scripts run in a sandbox.
inject(function() {
var pushState = history.pushState;
history.pushState = function on_pushState() {
window.postMessage('extension:pageUpdated', '*');
return pushState.apply(this, arguments);
};
var replaceState = history.replaceState;
history.replaceState = function on_replaceState() {
window.postMessage('extension:pageUpdated', '*');
return replaceState.apply(this, arguments);
};
});
// Do something when the extension is loaded into the page,
// and whenever we push/pop new pages.
window.addEventListener("message", function(event) {
if (event.data === 'extension:pageUpdated') {
load();
}
});
window.addEventListener("popstate", load);
load();
// End of code from https://github.com/thieman/github-selfies/blob/master/chrome/selfie.js
function load() {
chrome.runtime.sendMessage({action: 'load'}, function(response) {
initialize();
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
var fileActionDiv = mutation.target.querySelector(".file-actions");
if (fileActionDiv != null) {
initialize();
return;
}
});
});
var config = {
childList: true,
characterData: true,
subtree: true
};
// pass in the target node, as well as the observer options
var files = document.querySelector('#files');
if (files != null) {
observer.observe(files, config);
}
});
}
function any(array, predicate) {
for (var i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return true;
}
}
return false;
}
})();