-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
94 lines (85 loc) · 2.78 KB
/
popup.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
/*
function debug() {
console.log(arguments);
}
const error = debug;
*/
/*
* Without both these steps it doesn't work!
* It appears that the port is closed when the dev tools close.
* Do I need a 'background' page?
* 1. click on extension to show popup
* 2. right click on extension -> Inspect popup to show its dev tools
*/
/**
* tabFn should be chrome.tabs.insertCSS or chrome.tabs.executeScript
* adds debug logging and error handling
*/
function doTabFn(tabFn, tabId, details, callback) {
tabFn(tabId, details, result => {
if (chrome.runtime.lastError) {
console.log('doTabFn error: details =', details, ', error =', chrome.runtime.lastError.message);
} else {
console.log('doTabFn success: details =', details, ', result =', result);
callback(result);
}
});
};
function getActiveTab(callback) {
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
const tabId = tabs[0].id;
console.log('getActiveTab: tabId =', tabId);
callback(tabId);
});
}
function msgListener(msg) {
console.log('msgListener: msg =', msg);
if (msg.path) {
const div = $('#content div').has(':checked');
$('input', div).val(msg.path);
}
// port.postMessage("ack");
}
function doWithTab(tabId) {
const port = chrome.tabs.connect(tabId, {name: "port"});
port.onMessage.addListener(msgListener);
doTabFn(chrome.tabs.insertCSS, tabId, { code: '.highlightSelector { border: 1px solid red; }' }, () => {
doTabFn(chrome.tabs.executeScript, tabId, { file: "jquery-3.2.1.slim.min.js" }, () => {
doTabFn(chrome.tabs.executeScript, tabId, { file: "css-selector-generator.min.js" }, () => {
doTabFn(chrome.tabs.executeScript, tabId, { file: "injected.js" }, () => {
$('#content input[type=text]').change(e => {
const path = $(e.target).val();
console.log('createUI path change: path =', path);
port.postMessage({path: path});
});
});
});
});
});
}
const mkRadio = (name, id, text, val) => [
$('<input>').attr({ id: id, type: 'radio', name: name, value: val}),
$('<label>').attr({for: id, class: 'radioLabel'}).text(text)
];
const chkRadio = arr => {
arr[0].attr('checked', 'checked');
return arr;
};
// const radioVal = name => $(`input[name=${name}]:checked`).val();
function createUI() {
const widgets = [
{ id: 'price', name: 'Price'},
{ id: 'username', name: 'User Name'},
{ id: 'rating', name: 'Rating'}
].map((x, idx) => {
const arr = mkRadio('infoEle', 'infoEle_' + x.id, x.name, idx);
if (idx == 0) chkRadio(arr);
arr.push($('<input>').attr({ id: x.id, type: 'text', name: x.id, class: 'path' }));
return $('<div>').append(arr);
});
$('#content').append(widgets);
}
$(document).ready(() => {
createUI();
getActiveTab(doWithTab);
});