home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 89 / PIWD89.iso / pc / CONTENTS / DREAMWEAVER / UTILITIES / pawluk / FRAMEJAMMER / FRAMEJAMMER.JS < prev    next >
Encoding:
Text File  |  2003-10-05  |  9.8 KB  |  285 lines

  1. // Hal Pawluk 1999-2003   http://www.pawluk.com/public/
  2.  
  3.     var pageDOM="";
  4.     var Myscript = "";
  5.     var itsHere = false;        // tests for script in page
  6.     var itsThere = false;    // tests for script in frameset
  7.     var thisFrameset = "";
  8.     var thisFrame = "";
  9.     var activePage="";
  10.  
  11.     function isItHere(){        // called by onLoad in body tag
  12.         checkForDWfile();
  13.         pageDOM = dreamweaver.getDocumentDOM("document");
  14.         activePage = fileOnly(pageDOM.URL);
  15.         var theHeadNode = pageDOM.getElementsByTagName("HEAD");
  16.         if (theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){
  17.  
  18.             //--    look up frameset and frame names specified
  19.             var allScripts = pageDOM.getElementsByTagName("SCRIPT");
  20.             var jamScript = "";
  21.             if (allScripts) {
  22.                 for (i=0; i<allScripts.length; i++) {
  23.                     if (allScripts[i].innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){
  24.                         jamScript = allScripts[i].innerHTML;
  25.                         break;
  26.                 }    }    }            // end if-allscripts
  27.             jamScript = jamScript.substring(jamScript.indexOf('replace')+9);
  28.             var theSplit = jamScript.indexOf('?');
  29.             thisFrameset = jamScript.substring(0,theSplit);
  30.             jamScript = jamScript.substring(jamScript.lastIndexOf('~'));
  31.             thisFrame = jamScript.substring(1,jamScript.indexOf("'"));
  32.             //-- end of names lookup
  33.  
  34.             theChoices = document.entryForm.getFrame
  35.             for (var i=theChoices.options.length-1; i > 0 ;i--)        // reduce list to one item
  36.                 theChoices.options[i] = null;
  37.             theChoices.options[0].text = thisFrame;
  38.             
  39.             document.entryForm.theFrameset.options[0].text = thisFrameset;
  40.             itsHere = true;
  41.         }else{
  42.             thisFrameset = '';
  43.             thisFrame = '';
  44.             }
  45.         return itsHere;
  46.         }                                // end isItHere ---
  47.  
  48.     var jamFrameset = "";
  49.     var jamFramesetHome = "";
  50.     var jamFrame ="";
  51.     
  52.     function insertScript(){    // insert in page and handler in frameset
  53.         if (itsHere){
  54.             openDocRemove();
  55.             getFramesetHome();    
  56.             }
  57.         jamFrameset = document.entryForm.theFrameset.options[0].text;
  58.         theChoices = document.entryForm.getFrame
  59.     
  60.         if (jamFrameset=='Pick a target frameset for this page'){
  61.             alert('Please pick a target frameset.');
  62.             return;
  63.         }else if (theChoices.selectedIndex==0 && theChoices.options.length>1){
  64.             alert('Please specify a frame for this page.');
  65.             return;
  66.         }else{                        //we're good to go
  67.             jamFrame = theChoices.options[theChoices.selectedIndex].text;
  68.             if (jamFrame.indexOf("_") != -1)
  69.                 jamFrame = jamFrame.substring(0,jamFrame.indexOf("_"));
  70.  
  71.             Myscript = "\r"+ start + thisVersion +"\r"
  72.             + "if (window.name!='"+ jamFrame +"' && window.name!='booker_'\r"
  73.             + "         && !((self.innerHeight == 0) && (self.innerWidth == 0)))\r"
  74.             //next line was great but doesn't work in Netscape 4.08
  75.             //+ "        top.location.replace('"+ jamFrameset +"?'+ location.href +'~"+ jamFrame +"');\r"
  76.             
  77.             // activePage is updated in isItHere() and getFramesetHome()
  78.             + "        top.location.replace('"+ jamFrameset +"?"+  activePage   +"~"+ jamFrame +"');\r"
  79.  
  80.             + end + "\r"
  81.             openDocInsert();        // script into page
  82.  
  83.             // Now open the frameset target of the 'frameJammer' script above, write a function
  84.             // to handle 'jammed' pages, then save the frameset page.
  85.             
  86.             Myscript = "\r"+ start + thisVersion +"\r"
  87.             + "if (self != top)     // frame buster, may be removed\r"
  88.             + "    top.location.replace(self.location);\r\r"
  89.             + "function " + thisFnName + "(){\r"
  90.             + "    var framedPage = location.search;\r"
  91.             + "    if (framedPage.length > 1 && framedPage.indexOf(\"://\") == -1){\r"
  92.             + "        framedPage = framedPage.substring(1);\r"
  93.             + "        var theSplit = framedPage.lastIndexOf('~');\r"
  94.             + "        var thePage = framedPage.substring(0,theSplit);\r"
  95.             + "    // uncomment anchor code if required (see Help)\r"
  96.             + "    /*\r"
  97.             + "    if (thePage.indexOf('@') !=-1){  // checks for anchor\r"
  98.             + "        re=/(\\S+)@(\\S+)/\r"
  99.             + "        theArray = re.exec(thePage);\r"
  100.             + "        thePage=theArray[1]+'#'+theArray[2];\r"
  101.             + "        }\r"
  102.             + "    */\r"
  103.             + "        var theFrame = framedPage.substring(theSplit+1);\r"
  104.             + "        eval('top.frames[\"'+theFrame+'\"].location.replace(\"'+ thePage +'\");');\r"
  105.  
  106.             + "        }\r"
  107.             + "    }\r"
  108.             + end + "\r";
  109.             
  110.             pageDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
  111.             //pageDOM.body.setAttribute('onLoad','frameJammer_hp()');
  112.             closedDocInsert(pageDOM);
  113.             if (!itsThere)
  114.                 pageDOM.body.setAttribute('onLoad','frameJammer_hp()');
  115.             dreamweaver.saveDocument(pageDOM,pageDOM.URL);
  116.             window.close();
  117.             }                            // end good to go
  118.         }                              // end insertScript
  119.  
  120.     function getFramesetHome(){        // gets path to frameset, confirms it exists
  121.         // called by browseFile and insertScript, 'jamFramesetHome' used by checkFrames
  122.         activePage = fileOnly(pageDOM.URL);        
  123.         var pathToPage = pageDOM.URL;
  124.         pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));
  125.         jamFramesetHome = noSpaces(document.entryForm.theFrameset.options[0].text);
  126.         var preFix="";
  127.         while (jamFramesetHome.charAt(0) == "."){
  128.             jamFramesetHome = jamFramesetHome.substring(jamFramesetHome.indexOf("/")+1);
  129.             preFix = pathToPage.substring(pathToPage.lastIndexOf("/")) + preFix;
  130.             pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));    
  131.             }
  132.         jamFramesetHome = pathToPage +"/"+ jamFramesetHome;
  133.  
  134.         // added two lines
  135.         preFix = preFix.charAt(0)=='/'? preFix.substring(1)+"/" : preFix;
  136.         activePage = preFix + activePage;
  137.  
  138.         if ( !DWfile.exists( jamFramesetHome ) ){
  139.             alert("Sorry, can't find the file '"+ jamFrameset+"'");
  140.             return false;
  141.         }else 
  142.             return true;
  143.         }
  144.  
  145.  
  146.     var isDefault = -1;
  147.     function checkFrames(){
  148.         theChoices = document.entryForm.getFrame
  149.         for (var i=theChoices.options.length-1; i > 0 ;i--)
  150.             theChoices.options[i] = null;
  151.         var framesDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
  152.         theFrames = framesDOM.getElementsByTagName('FRAME')
  153.         if (theFrames.length == 0){
  154.             alert('Sorry, can\'t find any frames in this page');
  155.             document.entryForm.theFrameset.options[0].text = "Pick a target frameset for this page";
  156.             theChoices.options[0].text = '--- Frame names will be listed here ---';
  157.         }else{
  158.             theChoices.options[0].text = 'Now pick a frame for this page:';
  159.             var theCut = pageDOM.URL.lastIndexOf('/') + 1;
  160.             var thisPage = pageDOM.URL.substring(theCut);
  161.             isDefault = -1;
  162.             for (var i=0; i< theFrames.length; i++){
  163.                 if (theFrames[i].getAttribute('SRC').indexOf(thisPage) != -1){
  164.                     isDefault = i;
  165.                     break;
  166.             }    }    // end else theFrames.length
  167.             if (isDefault >= 0){
  168.                 theChoices.options[0].text = theFrames[isDefault].getAttribute('NAME');
  169.                 theChoices.options[0].selected = true;
  170.             }else{
  171.                 for (var i=0; i< theFrames.length; i++)
  172.                     theChoices.options[i+1] = new Option ( theFrames[i].getAttribute('NAME') );
  173.                     theChoices.options[0].selected = true;
  174.             }    }    //end isDefault
  175.         document.entryForm.theFrameset.options[0].selected = true; // 'prints' frameset name
  176.         }    //end checkFrames
  177.  
  178.  
  179.     function removeScript(){
  180.         if (itsHere){
  181.             openDocRemove();
  182.             window.close();
  183.         }else{
  184.             alert(thisFnName + ' is not in this page.');
  185.             return;
  186.         }    }
  187.  
  188.     //---------------------
  189.     
  190. function openDocInsert(){
  191.     theLocalDom=dreamweaver.getDocumentDOM("document");
  192.     scriptinsert(theLocalDom);
  193.     }
  194.     
  195. function openDocRemove(){
  196.     theLocalDom=dreamweaver.getDocumentDOM("document");
  197.     scriptdelete(theLocalDom);
  198.     }
  199.  
  200. function closedDocInsert(otherDom){
  201.     scriptinsert(otherDom);
  202.     }
  203.     
  204. function closedDocRemove(otherDom){
  205.     scriptdelete(otherDom);
  206.     }
  207.  
  208. function scriptinsert(thePageDom){
  209.     theHeadNode=thePageDom.getElementsByTagName("HEAD");
  210.     itsThere=theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase())!="-1";
  211.     if (itsThere);
  212.     else
  213.           theHeadNode.item(0).innerHTML += '<script language="javascript" type="text/javascript">' + Myscript + '</s'+'cript>';
  214.     }
  215.  
  216. function scriptdelete(thePageDom){
  217.     theHeadNode = thePageDom.getElementsByTagName("HEAD");
  218.     itsThere = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase()) != -1;    
  219.     if(itsThere){
  220.         theScriptNodes=thePageDom.getElementsByTagName("SCRIPT");
  221.         for(var i=0;i<theScriptNodes.length;i++){
  222.             var innerScript = theScriptNodes[i].innerHTML.toLowerCase();
  223.             if (innerScript.indexOf(codeToCheck.toLowerCase()) != -1){
  224.                 var beginsAt = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(start.toLowerCase());
  225.                 var endsAt   = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase());
  226.                 var theChunk = theHeadNode.item(0).innerHTML.substring(beginsAt,endsAt+end.length);
  227.                 theHeadNode.item(0).innerHTML = subtractString(theHeadNode.item(0).innerHTML, theChunk);
  228.                 var leavings = noSpaces(theScriptNodes[i].innerHTML);
  229.                 if (leavings=="<!--//-->")        // need to separate the tests or DW breaks
  230.                     theScriptNodes[i].outerHTML = "" ;
  231.                 else if (leavings=="")            // removes empty script tags
  232.                     theScriptNodes[i].outerHTML = "" ;
  233.                 break;
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239. // ---- support functions ----
  240.  
  241. function noSpaces(theString){
  242.     var theEnd = theString.length;
  243.     for (var i=0; i< theEnd; i++){
  244.         while ( theString.charCodeAt(i)<33 && i<theEnd){
  245.             theString = theString.substring(0,i) + theString.substring(i+1);
  246.             theEnd-- ;
  247.             }
  248.         }
  249.     return theString;
  250.     }
  251.     
  252. function subtractString(theString,theTakeaway){
  253.     var theSplit = theString.indexOf(theTakeaway);
  254.     if (theSplit != -1);{
  255.         var theLength = theTakeaway.length;
  256.         theString = theString.substring(0,theSplit) + theString.substring(theSplit+theLength);
  257.         }
  258.     return theString;
  259.     }
  260.     
  261. function padRight(theString,thePad,howLong){
  262.     while(theString.length < howLong)
  263.         theString += thePad;
  264.     return theString;
  265.     }
  266. function fileOnly(theString) {
  267.     var theSplit = theString.lastIndexOf ('/');
  268.     if (theSplit != -1)
  269.         return theString.substring(theSplit+1);
  270.     else
  271.         return "";
  272.     }
  273.     
  274. function checkForDWfile(){
  275.     if(typeof(DWfile)=="undefined"){
  276.         var message="Sorry, you need the DWfile library\r";
  277.         message+="You can download it from Macromedia's website";
  278.         alert(message);
  279.         dreamweaver.browseDocument("http://www.macromedia.com/support/dreamweaver/extend/dwfile/");
  280.         window.close();
  281.         return false;
  282.     }else
  283.         return nada="";
  284.     }
  285.