-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_script.js
90 lines (78 loc) · 2.23 KB
/
content_script.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
var options = {
convertToLocalImagePath: true
};
var defaultConfig = {
key: '-1',
name: 'default demo',
domain: '*',
code: 'console.log(window.location.href);'
};
var config = [defaultConfig];
var OPTION_KEY = 'noryalScriptable';
function reloadConfig(callback) {
chrome.storage.local.get({ noryalScriptable: JSON.stringify(config) }, function (storageData) {
var data = storageData[OPTION_KEY] || '[]';
if (data) {
config = JSON.parse(data);
for (var i = 0; i < config.length; i++) {
config[i].type = config[i].type || "click";
}
//console.log('load config: ' + config.length + ' items');
if (callback) {
callback();
}
}
});
}
$(function () {
// reload config and execute scripts for onLoad event
reloadConfig(function () {
executeForType('load');
});
$('a.extentionTrigger').on('click', function (e) {
var action = $(e.target).attr('action');
if (action == 'reloadConfig') {
reloadConfig();
}
});
});
function executeForType(type) {
GLOBAL_SCRIPTABLE_ENTRY = type;
if (config.length < 1) {
reloadConfig();
}
var name = '';
var found = false;
var url = window.location.href;
if (url) {
for (var i = 0; i < config.length; i++) {
var pattern = config[i].domain || '*';
pattern = pattern.replace(/\//g, '\\/');
pattern = pattern.replace(/\./g, '\\.');
pattern = pattern.replace(/\*/g, '.*');
pattern = pattern.replace(/\?/g, '.?');
if ((config[i].type == type || config[i].type == 'both') && config[i].status != 'disabled') {
var code = config[i].code;
name = config[i].name;
var regx = eval('/' + pattern + '/');
if (url.match(regx)) {
found = true;
eval(code);
}
}
}
}
}
function trigger() {
executeForType('click');
}
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (var key in changes) {
if (key == OPTION_KEY) {
reloadConfig();
}
var storageChange = changes[key];
//console.log('Storage key "%s" in namespace "%s" changed. Old value was "%s", new value is "%s".', key, namespace, storageChange.oldValue, storageChange.newValue);
}
});
extentionSetCommand("trigger();");