-
Notifications
You must be signed in to change notification settings - Fork 0
/
webview-injected.js
51 lines (45 loc) · 2.04 KB
/
webview-injected.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
document.addEventListener('DOMContentLoaded', (event) => {
// Webview injected log message
let log = (message) => { console.log('$webview-injected$ ' + message); };
// Electron import
const { ipcRenderer } = require('electron');
// Start webview injection
ipcRenderer.on('start-webview-injected', (event, sitejsondata, searchvalue) => {
// Inject jquery
let script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.4.1.min.js';
script.crossorigin = 'anonymous';
script.onload = script.onreadystatechange = () => {
$(document).ready(() => {
// Avoiding jQuery conflicts
let $wi = jQuery.noConflict(true);
// Inject CSS
let $injectedcssrules = $wi('<style type="text/css"></style>').appendTo('head');
$injectedcssrules.append('.webview-injected-highlight { background-color: lightyellow; }');
// Search on page
//$wi(`body > *:contains("${searchvalue}"):first`).addClass("webview-injected-highlight");
// Scroll to result
if ((typeof sitejsondata.resultscroll !== 'undefined') && (sitejsondata.resultscroll !== '')) {
$wi('body,html').animate(
{
scrollTop: $wi(sitejsondata.resultscroll).offset().top
},
800
);
} else {
$wi('body,html').animate(
{
scrollTop: 0
},
800
);
}
// Highlight result
if ((typeof sitejsondata.resulthighlight !== 'undefined') && (sitejsondata.resulthighlight !== '')) {
$wi(sitejsondata.resulthighlight).addClass("webview-injected-highlight");
}
});
};
document.body.appendChild(script);
});
});