-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSWFRightClick.js
163 lines (116 loc) · 5.64 KB
/
SWFRightClick.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ======================================================
SWFRightClick v1.0.20120227
https://github.com/pipwerks/SWFRightClick
Copyright (c) Philip Hutchison February 2012
MIT-style license: http://pipwerks.mit-license.org/
Notes:
* Designed to replace RightClick.js by Paulius Uza
(http://www.uza.lt) and Dan Florio (http://polygeek.com)
http://code.google.com/p/custom-context-menu/
* Uses same technique as RightClick.js but different
syntax and code approach
* Successfully tested in IE6-9, Safari, Firefox 3-10, and Chrome 17.
========================================================= */
var SWFRightClick = (function(){
//Utility function
var getTargetElement = function (e){
e = e || window.event;
return e.target || e.srcElement;
};
/*
capture(element, includeCtrlKey)
Disables the browser's default right-click handling when
clicking on the specified SWF. Uses ExternalInterface to
invoke ActionScript inside the SWF whenever the right-click
event takes place. Also disables default ctrl-click handling
unless specified otherwise by developer (set includeCtrlKey
argument to false).
Parameters:
element [string or DOM node, required]
* If string, pass the ID of the SWF you wish to interact with
* If node, pass reference to SWF's DOM node (<object> or <embed> element)
includeCtrlKey [boolean, optional]
If set to true, SWFRightClick will handle ctrl-clicks on SWF identically to
right-clicks. If set to false, will let browser perform default action.
Defaults to true if not specified.
*/
this.capture = function (element, includeCtrlKey) {
//If no element argument specified, end now.
if(!element){ return; }
//If user doesn't specify whether to check the control key, set default to true
includeCtrlKey = (typeof includeCtrlKey === "boolean") ? includeCtrlKey : true;
//Maintain a reference to our SWF, so we won't have to look it up over and over
//Accepts both string and node as argument
var theSWF = (element.nodeType && element.nodeType === 1) ? element : document.getElementById(element);
//Ensure the target element exists before continuing.
if(!theSWF){ return; }
//Auto-detect the element containing the <object> generated by SWFObject.
//All of the event handlers will be attached to this container.
var container = theSWF.parentNode;
if(container.addEventListener){
/*
W3C broswers: Webkit (Safari, Chrome), Gecko (FF), IE9+, Opera 7+
These browsers all support element.addEventListener, as well as
eventObject.stopPropagation and eventObject.preventDefault.
In some cases, IE9 will not support eventObject.preventDefault,
so a fallback `eventObject.returnValue = false` is supplied.
*/
container.addEventListener("mousedown", function (e){
//If this is a right-click on the SWF
//or a control-click if includeCtrlKey == true
if (getTargetElement(e) === theSWF && (e.button === 2 || (includeCtrlKey && e.ctrlKey))) {
//Prevent default behaviors: no browser context menu.
if(e.preventDefault){
e.preventDefault();
} else {
e.returnValue = false;
}
//Stop bubbling: Prevents the browser from wasting time.
e.stopPropagation();
//Since Flash won't know a right-click occured, we must inform it manually by
//invoking a custom callback function in your SWF via ExternalInterface
theSWF.rightClick();
}
}, true);
//The "true" argument above enables event capturing, preventing
//Flash Player from knowing a right-click occurred.
} else if(container.attachEvent){
/*
IE8 and lower don't support addEventListener, so
we must use attachEvent instead. Using attachEvent
is cleaner than "document.onmousedown = function ()" and
will not interfere with other event handlers, if any.
*/
container.attachEvent("onmousedown", function(e) {
//If this is a right-click on the SWF
//or a control-click if includeCtrlKey == true
if (getTargetElement(e) === theSWF && (e.button === 2 || (includeCtrlKey && e.ctrlKey))) {
//Prevent the Flash Player context menu from appearing by intercepting
//the click -- Flash Player never knows the click happened
container.setCapture();
//Since Flash won't know a right-click occured, we must inform it manually by
//invoking a custom callback function in your SWF via ExternalInterface
theSWF.rightClick();
}
});
//Stop intercepting the clicks, enabling Flash to detect clicks again.
container.attachEvent("onmouseup", function() {
container.releaseCapture();
});
//Prevent the browser's context menu from appearing,
//but ONLY when right-clicking or control-clicking the SWF
container.attachEvent("oncontextmenu", function(e){
return (getTargetElement(e) !== theSWF);
});
}
};
return this;
}());
//Wrapper for drop-in Adobe Captivate support
/*
RightClick = {
init: function(){
SWFRightClick.capture("Captivate");
}
};
*/