“保护标签页”- // ==UserScript==
- // @name tabProtect.uc.js
- // @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
- // @description tabProtect
- // @include main
- // @compatibility Firefox 2.0 3.0
- // @author Alice0775
- tabProtect = {
- debug: function(aMsg){
- Cc["@mozilla.org/consoleservice;1"]
- .getService(Ci.nsIConsoleService)
- .logStringMessage(aMsg.toString());
- },
- init: function(){
- //tabbrowser.xml銈掔疆銇嶆彌銇�
- //this.debug(gBrowser.removeTab.toString());
- gBrowser.removeTab_old = gBrowser.removeTab;
- gBrowser.removeTab =function(aTab){
- this._browsers = null; // invalidate cache
- if (aTab.localName != "tab")
- aTab = this.mCurrentTab;
- if (aTab.hasAttribute("tabProtect")) return;
- gBrowser.removeTab_old(aTab);
- }
- //this.debug(gBrowser.removeTab.toString());
- this.tabContextMenu();
- // CSS銈掗仼鐢�
- if(this.getVer()<3){
- var style = <![CDATA[
- .tab-close-button[hidden='true'] {
- display: none !important;
- }
- .tab-icon-protect{
- list-style-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAQUlEQVQ4jWNgGAXDADASUvDvOsN/fPJMlLqAhRhFTJqo/H/XKXQBsoFEuQDDVnIMQPcGXJxYA3C5hiwvUOwCZAAAlRcK7m+YgB4AAAAASUVORK5CYII=');
- }
- .tab-icon-protect[hidden='true'] {
- display: none !important;
- }
- ]]>.toString();
- }else{
- var style = <![CDATA[
- .tab-close-button[hidden='true'] {
- display: none !important;
- }
- .tabbrowser-tab.tabProtect {
- -moz-appearance: none;
- height: 22px;
- margin: 0px 0px 3px;
- padding: 0px 1px 1px;
- background-color:#00ff88;
- }
- .tabbrowser-tab.tabProtect[selected="true"] {
- height: 23px;
- margin: 0px 0px 3px;
- padding: 1px 1px 0px;
- background-color:#00ff88;
- }
- .tabbrowser-tab.tabProtect[selected="true"]:hover {
- height: 23px;
- margin: 0px 0px 3px;
- padding: 1px 1px 0px;
- background-color:#00ff88;
- }
- .tabbrowser-tab.tabProtect:not([selected="true"]) {
- height: 19px;
- margin: 0px 0px 5px;
- padding: 1px 0px 1px;
- background-color:#00ff88;
- }
- .tabbrowser-tab.tabProtect:not([selected="true"]):hover {
- height: 21px;
- margin: 0px 0px 5px;
- padding: 1px 1px 0px;
- background-color:#00ff88;
- } ]]>.toString();
- }
- var sspi = document.createProcessingInstruction(
- 'xml-stylesheet',
- 'type="text/css" href="data:text/css,' + encodeURI(style) + '"'
- );
- document.insertBefore(sspi, document.documentElement);
- sspi.getAttribute = function(name) {
- return document.documentElement.getAttribute(name);
- };
- //璧峰嫊鏅傘伄銈裤儢鐘舵厠寰╁厓
- var that = this;
- init(0);
- function init(i){
- if(i < gBrowser.mTabContainer.childNodes.length){
- var aTab = gBrowser.mTabContainer.childNodes[i];
- if(aTab.linkedBrowser.docShell.busyFlags
- || aTab.linkedBrowser.docShell.restoringDocument){
- setTimeout(init,1000,i);
- }else{
- that.restoreForTab(aTab);
- i++;
- setTimeout(init,0,i);
- }
- }else{
- }
- }
- gBrowser.tabContainer.addEventListener('TabMove', tabProtect.TabMove, false);
- gBrowser.tabContainer.addEventListener('SSTabRestored', tabProtect.restore,false);
- window.addEventListener('unload',function(){ tabProtect.uninit();},false)
- },
- uninit: function(){
- gBrowser.tabContainer.removeEventListener('TabMove', tabProtect.TabMove, false);
- gBrowser.tabContainer.removeEventListener('SSTabRestored', tabProtect.restore,false);
- // document.documentElement.removeEventListener('SubBrowserFocusMoved', function(){ tabProtect.init(); }, false);
- },
- getVer: function(){
- const Cc = Components.classes;
- const Ci = Components.interfaces;
- var info = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
- var ver = parseInt(info.version.substr(0,3),10);
- return ver;
- },
- //TAB D&D
- TabMove: function(aEvent){
- var aTab = aEvent.target;
- gBrowser.protectTabIcon(aTab);
- },
- tabContextMenu: function(){
- //tab context menu
- var tabContext = document.getAnonymousElementByAttribute(
- gBrowser, "anonid", "tabContextMenu");
- var menuitem = tabContext.appendChild(
- document.createElement("menuitem"));
- menuitem.id = "tabProtect";
- menuitem.setAttribute("type", "checkbox");
- menuitem.setAttribute("label", "\u4FDD\u62A4\u6807\u7B7E\u9875");
- menuitem.setAttribute("accesskey", "P");
- menuitem.setAttribute("oncommand","tabProtect.toggle(event);");
- tabContext.addEventListener('popupshowing',function(event){tabProtect.setCheckbox(event);},false);
- },
- restore: function(event){
- var aTab = event.originalTarget;
- tabProtect.restoreForTab(aTab);
- },
- restoreForTab: function(aTab){
- var ss = Components.classes["@mozilla.org/browser/sessionstore;1"].
- getService(Components.interfaces.nsISessionStore);
- var retrievedData = ss.getTabValue(aTab, "tabProtect");
- if(retrievedData){
- aTab.setAttribute('tabProtect',true);
- var closeButton = document.getAnonymousElementByAttribute(
- aTab, "anonid", "close-button");
- closeButton.setAttribute('hidden',true);
- }
- gBrowser.protectTabIcon(aTab);
- },
- toggle: function(event){
- var aTab = document.popupNode;
- while( aTab && aTab instanceof XULElement && aTab.localName !='tab'){
- aTab = aTab.parentNode;
- }
- if( !aTab || aTab.localName !='tab') return;
- gBrowser.protectTab(aTab);
- },
- setCheckbox: function(event){
- var menuitem = document.getElementById('tabProtect');
- var aTab = document.popupNode;
- while( aTab && aTab instanceof XULElement && aTab.localName !='tab'){
- aTab = aTab.parentNode;
- }
- if( !aTab || aTab.localName !='tab'){
- menuitem.setAttribute('hidden',true);
- return;
- }
- menuitem.setAttribute('hidden',false);
- if(aTab.hasAttribute('tabProtect') && aTab.getAttribute('tabProtect')){
- menuitem.setAttribute('checked', true);
- }else{
- menuitem.setAttribute('checked', false);
- }
- }
- }
- gBrowser.protectTab = function (aTab){
- var ss = Components.classes["@mozilla.org/browser/sessionstore;1"].
- getService(Components.interfaces.nsISessionStore);
- if ( aTab.hasAttribute("tabProtect") ){
- aTab.removeAttribute("tabProtect");
- ss.deleteTabValue(aTab, "tabProtect");
- }else{
- aTab.setAttribute("tabProtect", "true");
- ss.setTabValue(aTab, "tabProtect", true);
- }
- this.protectTabIcon(aTab);
- }
- gBrowser.protectTabIcon = function (aTab){
- const kXULNS =
- "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
- var closeButton = document.getAnonymousElementByAttribute(
- aTab, "anonid", "close-button");
- var image = document.getAnonymousElementByAttribute(
- aTab, "class", "tab-icon-protect");
- if ( aTab.hasAttribute("tabProtect") ){
- closeButton.setAttribute('hidden',true);
- if(!image){
- var stack = document.getAnonymousElementByAttribute(
- aTab, "class", "tab-icon");
- var image = document.createElementNS(kXULNS,'image');
- image.setAttribute('class','tab-icon-protect');
- if(stack) stack.appendChild(image);
- }
- aTab.setAttribute('class',aTab.getAttribute('class')+' tabProtect');
- image.removeAttribute('hidden');
- }else{
- closeButton.setAttribute('hidden',false);
- if(image){
- image.setAttribute('hidden', true);
- }
- aTab.setAttribute('class',aTab.getAttribute('class').replace(/\stabProtect/g,''));
- }
- }
- //document.documentElement.addEventListener('SubBrowserFocusMoved', function(){ tabProtect.init(); }, false);
- tabProtect.init();
复制代码 [ 本帖最后由 nettrottist 于 2008-11-18 08:35 编辑 ] |