OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 | 516 |
517 ext.backgroundPage = { | 517 ext.backgroundPage = { |
518 getWindow: function() | 518 getWindow: function() |
519 { | 519 { |
520 return safari.extension.globalPage.contentWindow; | 520 return safari.extension.globalPage.contentWindow; |
521 } | 521 } |
522 }; | 522 }; |
523 | 523 |
524 ext.onMessage = new MessageEventTarget(safari.application); | 524 ext.onMessage = new MessageEventTarget(safari.application); |
525 | 525 |
526 // TODO: Implement context menu | 526 var contextMenuItems = []; |
| 527 var isContextMenuHidden = true; |
527 ext.contextMenus = { | 528 ext.contextMenus = { |
528 create: function(title, contexts, onclick) {}, | 529 addMenuItem: function(title, contexts, onclick) |
529 removeAll: function(callback) {} | 530 { |
| 531 contextMenuItems.push({ |
| 532 id: String(contextMenuItems.length), |
| 533 title: title, |
| 534 contexts: contexts, |
| 535 onclick: onclick |
| 536 }); |
| 537 this.showMenuItems(); |
| 538 }, |
| 539 removeMenuItems: function() |
| 540 { |
| 541 contextMenuItems = []; |
| 542 this.hideMenuItems(); |
| 543 }, |
| 544 showMenuItems: function() |
| 545 { |
| 546 isContextMenuHidden = false; |
| 547 }, |
| 548 hideMenuItems: function() |
| 549 { |
| 550 isContextMenuHidden = true; |
| 551 } |
530 }; | 552 }; |
531 | 553 |
| 554 // Create context menu items |
| 555 safari.application.addEventListener("contextmenu", function(event) |
| 556 { |
| 557 if (isContextMenuHidden) |
| 558 return; |
| 559 |
| 560 var context = event.userInfo.tagName.toLowerCase(); |
| 561 if (context == "img") |
| 562 context = "image"; |
| 563 if (!event.userInfo.srcUrl) |
| 564 context = null; |
| 565 |
| 566 for (var i = 0; i < contextMenuItems.length; i++) |
| 567 { |
| 568 // Supported contexts are: all, audio, image, video |
| 569 var menuItem = contextMenuItems[i]; |
| 570 if (menuItem.contexts.indexOf("all") == -1 && menuItem.contexts.indexOf(co
ntext) == -1) |
| 571 continue; |
| 572 |
| 573 event.contextMenu.appendContextMenuItem(menuItem.id, menuItem.title); |
| 574 } |
| 575 }, false); |
| 576 |
| 577 // Handle context menu item clicks |
| 578 safari.application.addEventListener("command", function(event) |
| 579 { |
| 580 for (var i = 0; i < contextMenu.length; i++) |
| 581 { |
| 582 if (contextMenu[i].id == event.command) |
| 583 { |
| 584 contextMenu[i].onclick(event.userInfo.srcUrl, new Tab(safari.application
.activeBrowserWindow.activeTab)); |
| 585 break; |
| 586 } |
| 587 } |
| 588 }, false); |
| 589 |
532 // Safari will load the bubble once, and then show it everytime the icon is | 590 // Safari will load the bubble once, and then show it everytime the icon is |
533 // clicked. While Chrome loads it everytime you click the icon. So in order to | 591 // clicked. While Chrome loads it everytime you click the icon. So in order to |
534 // force the same behavior in Safari, we are going to reload the page of the | 592 // force the same behavior in Safari, we are going to reload the page of the |
535 // bubble everytime it is shown. | 593 // bubble everytime it is shown. |
536 if (safari.extension.globalPage.contentWindow != window) | 594 if (safari.extension.globalPage.contentWindow != window) |
537 safari.application.addEventListener("popover", function() | 595 safari.application.addEventListener("popover", function() |
538 { | 596 { |
539 document.documentElement.style.display = "none"; | 597 document.documentElement.style.display = "none"; |
540 document.location.reload(); | 598 document.location.reload(); |
541 }, true); | 599 }, true); |
542 })(); | 600 })(); |
OLD | NEW |