-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathsafariInject.ts
54 lines (47 loc) · 1.69 KB
/
safariInject.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
import {Constants} from "../../constants";
import {ClipperInject, ClipperInjectOptions} from "../clipperInject";
import {ContextItemParameter, ContextType} from "./safariContext";
import {SafariContentMessageHandler} from "./safariMessageHandler";
declare var safari;
/*
* Since there is no way for a Safari Extension to inject scripts On Demand,
* this code is injected into every page, and waits for a command from the extension
* to execute the code
*/
// This is injected into every page loaded (including frames), but we only want to set the listener on the main document
if (window.top === window) {
safari.self.addEventListener("message", (event) => {
if (event.name === Constants.FunctionKeys.invokeClipper) {
let options: ClipperInjectOptions = {
frameUrl: event.message,
enableAddANote: true,
enableEditableTitle: true,
enableRegionClipping: true,
extMessageHandlerThunk: () => { return new SafariContentMessageHandler(); }
};
ClipperInject.main(options);
}
});
// Send 'what the user clicked on' back to the extension's contextmenu event
document.addEventListener("contextmenu", (event: Event) => {
let objToSend: ContextItemParameter;
if (!window.getSelection().isCollapsed) {
objToSend = {
type: ContextType.Selection,
parameters: {}
};
} else if (event.target && (<Node>event.target).nodeName === "IMG") {
let node = event.target as HTMLImageElement;
objToSend = {
type: ContextType.Img,
parameters: {
src: node.src
}
};
}
if (objToSend) {
// You can only set one user info context object, and it can not be an object!
safari.self.tab.setContextMenuEventUserInfo(event, JSON.stringify(objToSend));
}
}, false);
}