-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathchromeExtension.ts
77 lines (65 loc) · 2.72 KB
/
chromeExtension.ts
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
import {ClientType} from "../../clientType";
import {WebExtension} from "../webExtensionBase/webExtension";
function sendMessageToContentScript(tabs, msg) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, { bMessage: msg }, function (response) {
if (msg !== "GET_ALL_YOUTUBE_DETAILS") {
console.log(response.cMessage);
}
});
}
}
/****** START CODE TO COMMUNICATE FROM CONTENT TO BACKGROUND SCRIPT ******/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
// Received all details from content script, now send back to native application
sendMessageToNativeApplication({
youtubeURL: request.youtubeURL,
documentTitle: request.documentTitle,
videoId: request.videoId,
streamPlayer: request.streamPlayer,
pictureOfVideo: request.pictureOfVideo,
transcript: request.transcript
});
}
);
/****** END CODE TO COMMUNICATE FROM CONTENT TO BACKGROUND SCRIPT ******/
/****** START CODE TO COMMUNICATE BETWEEN EXTENSION AND NATIVE APPLICATION ******/
let hostName = 'com.microsoft.onenote.stickynotes';
let port = chrome.runtime.connectNative(hostName);
function sendMessageToNativeApplication(msg) {
port.postMessage(msg);
}
port.onMessage.addListener((response) => {
console.log('Received from native application: ' + JSON.stringify(response));
console.log('window.location.href as known to the current background script is: ' + window.location.href);
/****** START CODE TO COMMUNICATE FROM BACKGROUND TO CONTENT SCRIPT ******/
/**
* This is a background script and cannot access the DOM of the current page.
* To access the DOM of the current page, we need to send a message to the
* content script.
*/
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
/*
console.log('Getting youtube url from content script...');
sendMessageToContentScript(tabs, "GET_YOUTUBE_URL");
console.log('Getting video id from content script...');
sendMessageToContentScript(tabs, "GET_VIDEO_ID");
console.log('Getting stream player from content script...');
sendMessageToContentScript(tabs, "GET_STREAM_PLAYER");
*/
console.log('Getting all details from content script and sending back to native application...');
sendMessageToContentScript(tabs, "GET_ALL_YOUTUBE_DETAILS");
});
/****** END CODE TO COMMUNICATE FROM BACKGROUND TO CONTENT SCRIPT ******/
});
port.onDisconnect.addListener(() => {
console.log('Disconnected');
});
/****** END CODE TO COMMUNICATE BETWEEN EXTENSION AND NATIVE APPLICATION ******/
WebExtension.browser = chrome;
let clipperBackground = new WebExtension(ClientType.ChromeExtension, {
debugLoggingInjectUrl: "chromeDebugLoggingInject.js",
webClipperInjectUrl: "chromeInject.js",
pageNavInjectUrl: "chromePageNavInject.js"
});