UdCapture.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. 
  2. ;(function () {
  3. var UDCAPTURE_NEWVERSION = "2.0.1"; //当前最新的控件版本号
  4. var UDCAPTURE_LICENSE = ""; //注册授权许可号
  5. //安装文件
  6. var UDCAPTURE_SETUP_32 = "/soft/udcapture/UdCapture.msi?" + UDCAPTURE_NEWVERSION;
  7. var UDCAPTURE_SETUP_64 = "/soft/udcapture/UdCapture64.msi?" + UDCAPTURE_NEWVERSION;
  8. var UDCAPTURE_SETUP_CRX = "/soft/udcapture/UdCaptureCrx.msi?" + UDCAPTURE_NEWVERSION;
  9. var supportActiveX = (window.ActiveXObject !== undefined); //是否支持ActiveX,IE
  10. function UdCapture(options) {
  11. var that = this;
  12. //that.el = typeof el == 'object' ? el : document.getElementById(el);
  13. that.options = options || {};
  14. //设置默认属性
  15. that.IsReady = false;
  16. if(that.options.AutoMinimize)
  17. that.AutoMinimize = that.options.AutoMinimize;
  18. else
  19. that.AutoMinimize = false;
  20. if(that.options.License)
  21. that.License = that.options.License;
  22. else
  23. that.License = UDCAPTURE_LICENSE;
  24. if(that.options.FileField) that.FileField = that.options.FileField;
  25. if(that.options.FileName) that.FileName = that.options.FileName;
  26. if(that.options.TipInfo) that.TipInfo = that.options.TipInfo;
  27. if(!that.options.OnClick){
  28. that.options.OnClick = function(){
  29. that.startCapture();
  30. };
  31. }
  32. //if(that.el.attachEvent)
  33. // that.el.attachEvent('onclick', that.options.OnClick);
  34. //else
  35. // that.el.addEventListener("click", that.options.OnClick, false);
  36. that.Init();
  37. }
  38. UdCapture.prototype = {
  39. IsReady:false,
  40. Init:function(){
  41. var that = this;
  42. if (supportActiveX) {//if IE
  43. var ctlObj = document.getElementById("udCaptureCtl");
  44. if(ctlObj == null){
  45. ctlObj = document.createElement("object");
  46. ctlObj.id = "udCaptureCtl";
  47. ctlObj.setAttribute("classid","CLSID:0FAE7655-0200-4DEE-9620-CD7ED969B3F2");
  48. ctlObj.setAttribute("width",0);
  49. ctlObj.setAttribute("height",0);
  50. document.body.appendChild(ctlObj);
  51. }
  52. if (typeof ctlObj.IsReady == "undefined")
  53. return;
  54. else{
  55. if(that.options.OnCaptureCanceled) _addEvent(ctlObj, "OnCaptureCanceled", _onCaptureCanceled);
  56. if(that.options.OnCaptureCompleted) _addEvent(ctlObj, "OnCaptureCompleted", _onCaptureCompleted);
  57. if(that.options.OnUploadCompleted) _addEvent(ctlObj, "OnUploadCompleted", _onUploadCompleted);
  58. if(that.options.OnUploadFailed) _addEvent(ctlObj, "OnUploadFailed", _onUploadFailed);
  59. that._udCapCtl = ctlObj;
  60. that._version = ctlObj.GetVersion();
  61. }
  62. }
  63. else if(_neetCrx()){
  64. if(document.getElementById("UdCaptureCrx") == null)
  65. return;
  66. else{
  67. that._udCapCtl = new UdCaptureCrx();
  68. that._version = that._udCapCtl.GetVersion();
  69. }
  70. }else{
  71. if(navigator.plugins)
  72. navigator.plugins.refresh(false);
  73. var udCaptureMimeType = "application/udcapture-plugin"
  74. var plugin = (navigator.mimeTypes && navigator.mimeTypes[udCaptureMimeType]) ? navigator.mimeTypes[udCaptureMimeType].enabledPlugin : 0;
  75. if (plugin) {
  76. var pluginVersion = "v1.0.0";
  77. var words = plugin.description.split(" ");
  78. if (words[words.length - 1].substring(0, 1) == "v")
  79. pluginVersion = words[words.length - 1];
  80. if (pluginVersion.substring(0, 1) == 'v')
  81. pluginVersion = pluginVersion.substring(1, pluginVersion.length);
  82. that._version = pluginVersion;
  83. }
  84. var ctlObj = document.getElementById("udCapturePlugin");
  85. if(ctlObj == null){
  86. ctlObj = document.createElement("embed");
  87. ctlObj.id = "udCapturePlugin";
  88. ctlObj.setAttribute("type",udCaptureMimeType);
  89. ctlObj.setAttribute("width",0);
  90. ctlObj.setAttribute("height",0);
  91. document.body.appendChild(ctlObj);
  92. }
  93. if (typeof ctlObj.IsReady == "undefined"){
  94. return;
  95. }
  96. else{
  97. if(that.options.OnCaptureCanceled) ctlObj.OnCaptureCanceled ="_onCaptureCanceled";
  98. if(that.options.OnCaptureCompleted) ctlObj.OnCaptureCompleted ="_onCaptureCompleted";
  99. if(that.options.OnUploadCompleted) ctlObj.OnUploadCompleted ="_onUploadCompleted";
  100. if(that.options.OnUploadFailed) ctlObj.OnUploadFailed ="_onUploadFailed";
  101. that._udCapCtl = ctlObj;
  102. that._version = ctlObj.GetVersion();
  103. }
  104. }
  105. that.IsReady = true;
  106. _udCapLib = that;
  107. },
  108. StopCapture:function(){
  109. this._udCapCtl.StopCapture();
  110. $('#toolsBar .tools-mapshot').removeClass('cur');
  111. },
  112. StartCapture:function(){
  113. var that = this;
  114. if (that.InstallCheck()) {
  115. that._udCapCtl.StartCapture();
  116. }
  117. },
  118. Capture:function(){
  119. var that = this;
  120. if (that.InstallCheck()) {
  121. that._udCapCtl.Capture();
  122. }
  123. },
  124. CaptureScreen:function(){
  125. var that = this;
  126. if (that.InstallCheck()) {
  127. that._udCapCtl.CaptureScreen();
  128. }
  129. },
  130. CaptureWindow:function(n){
  131. var that = this;
  132. if (that.InstallCheck()) {
  133. that._udCapCtl.CaptureWindow(n);
  134. }
  135. },
  136. CaptureRect:function(left,top,width,height){
  137. var that = this;
  138. if (that.InstallCheck()) {
  139. that._udCapCtl.CaptureRect(left,top,width,height);
  140. }
  141. },
  142. GetVersion:function(){
  143. return this._version;
  144. },
  145. GetImageData:function(){
  146. return this._udCapCtl.GetImageData();
  147. },
  148. Upload:function(url,params){
  149. this._udCapCtl.Upload(url,params);
  150. },
  151. //检查是否安装了插件
  152. InstallCheck: function() {
  153. var that = this;
  154. if (!that._udCapCtl)
  155. that.Init();
  156. if(!that._udCapCtl){
  157. if (confirm("您尚未安装屏幕截图控件,是否现在进行安装?\r\n安装完成后请重新刷新浏览器。")) {
  158. that.StartSetup();
  159. that.Destory();
  160. }
  161. return false;
  162. }
  163. else if(_checkNewVersion(that._version)){
  164. if (confirm("屏幕截图控件有新版本v" + UDCAPTURE_NEWVERSION + ",需要升级后才能使用。\r\n是否现在进行升级?")) {
  165. that.StartSetup();
  166. that.Destory();
  167. }
  168. return false;
  169. }
  170. that._udCapCtl.AutoMinimize = that.AutoMinimize;
  171. if(that.License) that._udCapCtl.License = that.License;
  172. if(that.PostUrl) that._udCapCtl.PostUrl = that.PostUrl;
  173. if(that.FileField) that._udCapCtl.FileField = that.FileField;
  174. if(that.FileName) that._udCapCtl.FileName = that.FileName;
  175. if(that.PostParams) that._udCapCtl.PostParams = that.PostParams;
  176. if(that.TipInfo) that._udCapCtl.TipInfo = that.TipInfo;
  177. that._udCapCtl.WebCodePage = that.WebCodePage;
  178. that._udCapCtl.IsUtf8Url = that.IsUtf8Url;
  179. that._udCapCtl.IsUtf8Data = that.IsUtf8Data;
  180. if(that.options.OnBeforeCapture) that.options.OnBeforeCapture.call(that);
  181. return true;
  182. },
  183. //下载安装文件进行安装
  184. StartSetup: function(){
  185. var iframe = document.getElementById("udCaptureSetupFrame");
  186. if(iframe == null){
  187. iframe = document.createElement("iframe");
  188. iframe.id = "udCaptureSetupFrame";
  189. iframe.style.display = "none";
  190. document.body.appendChild(iframe);
  191. }
  192. var setupFile = UDCAPTURE_SETUP_32;
  193. if (supportActiveX && (window.navigator.platform == "Win64" || window.navigator.cpuClass == "x64"))//64位IE浏览器安装文件
  194. setupFile = UDCAPTURE_SETUP_64
  195. if(_neetCrx())
  196. setupFile = UDCAPTURE_SETUP_CRX
  197. iframe.setAttribute("src",setupFile);
  198. if(this.options.OnStartSetup)this.options.OnStartSetup.call(this,setupFile);
  199. },
  200. Destory:function(){
  201. var that = this;
  202. if(that._udCapCtl){
  203. document.body.removeChild(that._udCapCtl);
  204. that._udCapCtl = null;
  205. }
  206. that.IsReady = false;
  207. }
  208. }
  209. //IE事件注册
  210. function _addEvent(element, type, handler) {
  211. if (element.attachEvent) {
  212. element.attachEvent(type, handler);
  213. } else {
  214. _attachIE11Event(element, type, handler);
  215. }
  216. };
  217. //单独处理IE11的事件
  218. function _attachIE11Event(obj, eventId, _functionCallback) {
  219. var nameFromToStringRegex = /^function\s?([^\s(]*)/;
  220. var paramsFromToStringRegex = /\(\)|\(.+\)/;
  221. var params = _functionCallback.toString().match(paramsFromToStringRegex)[0];
  222. var functionName = _functionCallback.name || _functionCallback.toString().match(nameFromToStringRegex)[1];
  223. var handler = document.createElement("script");
  224. handler.setAttribute("for", obj.id);
  225. handler.event = eventId + params;
  226. handler.appendChild(document.createTextNode(functionName + params + ";"));
  227. document.body.appendChild(handler);
  228. };
  229. //检查是否有新版本
  230. function _checkNewVersion(instVer){
  231. var newVer = UDCAPTURE_NEWVERSION.split(".");
  232. var curVer = instVer.split(".");
  233. if (parseInt(newVer[0]) > parseInt(curVer[0]))
  234. return true;
  235. if (parseInt(newVer[0]) == parseInt(curVer[0]) && parseInt(newVer[1]) > parseInt(curVer[1]))
  236. return true;
  237. if (parseInt(newVer[0]) == parseInt(curVer[0]) && parseInt(newVer[1]) == parseInt(curVer[1])
  238. && parseInt(newVer[2]) > parseInt(curVer[2]))
  239. return true;
  240. return false;
  241. };
  242. //判断是否需要Crx安装
  243. function _neetCrx(){
  244. var agent = window.navigator.userAgent.toLowerCase();
  245. var isQQBrowser = agent.indexOf("qqbrowser") != -1;
  246. if(isQQBrowser)
  247. return false;
  248. var isChrome = agent.indexOf("chrome") != -1;
  249. if(isChrome) {
  250. if(chrome&&chrome.runtime){
  251. var gsChromeVer=""+(/chrome\/((\d|\.)+)/i.test(agent)&&RegExp["$1"]);
  252. if(gsChromeVer != "false"){
  253. var mainVer = parseInt(gsChromeVer);
  254. if(mainVer>41)
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. };
  261. //Crx插件处理类
  262. function UdCaptureCrx(){
  263. var _imageData = "";
  264. document.addEventListener('UdCaptureEventCallBack', function(evt) {
  265. if(evt.detail.EventName == "OnCaptureCanceled")_onCaptureCanceled();
  266. else if(evt.detail.EventName == "OnCaptureCompleted"){
  267. _imageData = evt.detail.arg2;
  268. _onCaptureCompleted(evt.detail.arg1,_imageData);
  269. }
  270. else if(evt.detail.EventName == "OnUploadCompleted")_onUploadCompleted(evt.detail.arg1);
  271. else if(evt.detail.EventName == "OnUploadFailed")_onUploadFailed();
  272. });
  273. this.PageUrl = location.href;
  274. this.WinName = document.title + " - Google Chrome";
  275. this.GetVersion = function(){
  276. return document.getElementById("UdCaptureCrx").getAttribute("version");
  277. };
  278. this.GetImageData = function(){
  279. return _imageData;
  280. };
  281. this.ExecCommand = function(){
  282. var that = this;
  283. var json = JSON.stringify(that);
  284. var evt = document.createEvent("CustomEvent");
  285. evt.initCustomEvent('UdCaptureEvent', true, false, json);
  286. document.dispatchEvent(evt);
  287. };
  288. this.StopCapture = function(){
  289. this.Command = "StopCapture";
  290. this.ExecCommand();
  291. };
  292. this.StartCapture = function(){
  293. this.Command = "StartCapture";
  294. this.ExecCommand();
  295. };
  296. this.Capture = function(){
  297. this.Command = "Capture";
  298. this.ExecCommand();
  299. };
  300. this.CaptureScreen = function(){
  301. this.Command = "CaptureScreen";
  302. this.ExecCommand();
  303. };
  304. this.CaptureWindow = function(n){
  305. this.Command = "CaptureWindow";
  306. if(typeof(n) != "undefined");
  307. this.CaptureWinIndex = n;
  308. this.ExecCommand();
  309. };
  310. this.CaptureRect = function(left,top,width,height){
  311. this.Command = "CaptureRect";
  312. this.CaptureLeft = left;
  313. this.CaptureTop = top;
  314. this.CaptureWidth = width;
  315. this.CaptureHeight = height;
  316. this.ExecCommand();
  317. };
  318. this.Upload = function(url,params){
  319. this.Command = "Upload";
  320. this.PostUrl = url;
  321. this.PostParams = params;
  322. this.ExecCommand();
  323. };
  324. }
  325. /**
  326. * Factory method for creating a UdCapture object
  327. *
  328. * @param {Element} The el to listen on
  329. * @param {Object} [options={}] The options to override the defaults
  330. */
  331. UdCapture.Attach = function (el, options) {
  332. return new UdCapture(el, options);
  333. };
  334. if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
  335. // AMD. Register as an anonymous module.
  336. define(function () {
  337. return UdCapture;
  338. });
  339. } else if (typeof module !== 'undefined' && module.exports) {
  340. module.exports = UdCapture.Attach;
  341. module.exports.UdCapture = UdCapture;
  342. } else {
  343. window.UdCapture = UdCapture;
  344. }
  345. }());
  346. //NPAPI的事件处理
  347. var _udCapLib
  348. function _onCaptureCanceled(){
  349. if(_udCapLib.options.OnCaptureCanceled)
  350. _udCapLib.options.OnCaptureCanceled.call(_udCapLib);
  351. }
  352. function _onCaptureCompleted(file){
  353. if(_udCapLib.options.OnCaptureCompleted){
  354. var imgData = _udCapLib.GetImageData();
  355. _udCapLib.options.OnCaptureCompleted.call(_udCapLib,file,imgData);
  356. }
  357. }
  358. function _onUploadCompleted(responseText){
  359. if(_udCapLib.options.OnUploadCompleted)
  360. _udCapLib.options.OnUploadCompleted.call(_udCapLib,responseText);
  361. }
  362. function _onUploadFailed(){
  363. if(_udCapLib.options.OnUploadFailed)
  364. _udCapLib.options.OnUploadFailed.call(_udCapLib);
  365. }