home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 89 / PIWD89.iso / pc / CONTENTS / DREAMWEAVER / UTILITIES / pawluk / FRAMEBUSTER / HP_INSERTCODE.JS < prev   
Text File  |  2000-06-16  |  7KB  |  233 lines

  1. // _insertCode.js
  2. // Based on work by Andrew Wooldridge, Massimo Foti, Danny Goodman, others.
  3. // Hal Pawluk 1999    hal@pawluk.com   http://www.pawluk.com/public/
  4.  
  5. function openDocInsert(){
  6. // insert in the currently open file
  7.     theLocalDom=dreamweaver.getDocumentDOM("document");
  8.     scriptinsert(theLocalDom,'openDoc');
  9.     window.close();
  10.     }
  11.  
  12. function closedDocInsert(theLocalDom){
  13. // insert in a specific single closed file
  14.     scriptinsert(theLocalDom);
  15.     window.close();
  16.     }
  17.  
  18. function openDocRemove(){
  19. // delete from the currently open file
  20.     theLocalDom=dreamweaver.getDocumentDOM("document");
  21.     scriptdelete(theLocalDom);
  22.     window.close();
  23.     }
  24.  
  25. function scriptinsert(thePageDom,target){    // --------------------
  26.  
  27.     theHeadNode=thePageDom.getElementsByTagName("HEAD");
  28.     isThere=theHeadNode.item(0).innerHTML.indexOf(codeToCheck)!="-1";
  29.     
  30.     if(isThere){                    // if the function is here, alert only
  31.         if(target=='openDoc')        // working on a single file
  32.             alert(activePage + ' - The script is already here');
  33.     }else{        //****
  34.           theHeadNode.item(0).innerHTML += "<script>" + Myscript + "</s"+"cript>";
  35.            }        //****
  36.     }        // end of scriptinsert function  -----------------------
  37.  
  38.  
  39. function scriptdelete(thePageDom){    // -----------------------------
  40.  
  41.     theHeadNode = thePageDom.getElementsByTagName("HEAD");
  42.     isThere = theHeadNode.item(0).innerHTML.indexOf(codeToCheck) != -1;    
  43.  
  44.     if(isThere){    //If the code in inside the page
  45.         theScriptNodes=thePageDom.getElementsByTagName("SCRIPT");
  46.  
  47.         // find the script with the code and remove it
  48.         for(i=0;i<theScriptNodes.length;i++){
  49.             if (theScriptNodes[i].innerHTML.indexOf(codeToCheck) != -1){
  50.                 theHeadNode.item(0).innerHTML = subtractString(theHeadNode.item(0).innerHTML, theScriptNodes[i].outerHTML);
  51.                 break;
  52.                 }
  53.             }
  54.         }
  55.     }        // end scriptdelete function   --------------------------------
  56.  
  57.  
  58. function allSiteInsert(){    // -----------------------------
  59. // sitewide insert, not used by all extensions
  60.     checkForDWfile();
  61.     
  62.     var theSiteRoot=dreamweaver.getSiteRoot();
  63.     
  64.     if( !theSiteRoot )
  65.         alert("You need to define a site first");
  66.     else{
  67.         dirArray    = new Array();    fileArray   = new Array();
  68.         dirPointer  = 0;            filePointer = 0;
  69.         dirArray[0] = theSiteRoot;
  70.  
  71.         // are there directories in the site?
  72.            haveDirectories = DWfile.listFolder(theSiteRoot + "*","directories");
  73.  
  74.         if(haveDirectories != ""){    
  75.             for(x=0; x<dirArray.length; x++){
  76.                 dirArray  = addToDirsArray(dirArray[x],dirArray);
  77.                 fileArray = addToFilesArray(dirArray[x],fileArray,"*.ht*");
  78.                 }
  79.         }else{
  80.             // are there suitable "*.ht*" files in the site?
  81.             haveFiles = DWfile.listFolder(theSiteRoot +"/*.ht*","files");
  82.         
  83.             if(haveFiles != ""){
  84.                 for(z=0; z<dirArray.length; z++){
  85.                     fileArray = addToFilesArray(dirArray[z],fileArray,"*.ht*");
  86.                     }
  87.                 }
  88.             }
  89.             
  90.         //Get all the files, do our job and save all
  91.         for(h=0; h<fileArray.length; h++){
  92.             tempDOM = dreamweaver.getDocumentDOM(fileArray[h]);
  93.             scriptinsert(tempDOM);
  94.             dreamweaver.saveDocument(tempDOM,fileArray[h]);
  95.             }
  96.         }        // end else there is a site root
  97.     window.close();
  98.     }        // end allSiteInsert function   --------------------------------
  99.  
  100.  
  101. function allSiteDelete(){        // -----------------------------------------
  102. // sitewide delete, not used by all extensions
  103.     checkForDWfile();
  104.  
  105.     var theSiteRoot=dreamweaver.getSiteRoot();
  106.     if( !theSiteRoot )
  107.         alert("You need to define a site first");
  108.     else{
  109.         dirArray = new Array();    fileArray = new Array();
  110.         dirPointer = 0;            filePointer = 0;
  111.         dirArray[0] = theSiteRoot;
  112.     
  113.         // are there directories in the site?
  114.         haveDirectories = DWfile.listFolder(theSiteRoot + "*","directories");
  115.         
  116.         if(haveDirectories != ""){    
  117.             for(x=0; x<dirArray.length; x++){
  118.                 dirArray = addToDirsArray(dirArray[x],dirArray);
  119.                 fileArray = addToFilesArray(dirArray[x],fileArray,"*.ht*");
  120.                 }
  121.         }else{
  122.             // are there suitable "*.ht*" files in the site?
  123.             haveFiles = DWfile.listFolder(theSiteRoot +"/*.ht*","files");
  124.  
  125.             if(haveFiles != ""){
  126.                 for(z=0; z<dirArray.length; z++){
  127.                     fileArray = addToFilesArray(dirArray[z],fileArray,"*.ht*");
  128.                     }
  129.                 }
  130.             }
  131.         
  132.         // do our job and save all
  133.         for(h=0; h<fileArray.length; h++){
  134.             tempDOM = dreamweaver.getDocumentDOM(fileArray[h]);
  135.             scriptdelete(tempDOM);
  136.             dreamweaver.saveDocument(tempDOM,fileArray[h]);
  137.             }
  138.         }        // end else there is a site root
  139.     window.close();
  140.     }        // end allSiteDelete  ---------------------------------
  141.  
  142.  
  143. // -------------- Check for DWfile Functions ---------------------- 
  144.  
  145. function checkForDWfile(){
  146.     if(typeof(DWfile)=="undefined"){
  147.     message="Sorry, you need the DWfile library\r";
  148.     message+="You can download it from Macromedia's website";
  149.     alert(message);
  150.     dreamweaver.browseDocument("http://www.macromedia.com/support/dreamweaver/extend/dwfile/");
  151.     window.close();
  152.     return false;
  153.     }
  154.     else{
  155.     return nada="";
  156.     }
  157. }
  158.  
  159. // ---------- Generic Site Functions by Andrew Wooldridge --------- 
  160.  
  161. function addToDirsArray(startPath,oldDirArray){
  162.     tempDirArray=DWfile.listFolder(startPath+"*","directories");
  163.     for(k=0;k<tempDirArray.length;k++){
  164.         returnDirArray = oldDirArray;
  165.         returnDirArray[returnDirArray.length]=startPath+tempDirArray[k]+"/";
  166.         }
  167.     return returnDirArray;
  168. }
  169.  
  170. function addToFilesArray(startPath,oldFileArray,fileMask){
  171.     tempFileArray=DWfile.listFolder(startPath + fileMask,"files");
  172.     returnFileArray = oldFileArray;
  173.     for(j=0;j<tempFileArray.length;j++){
  174.         returnFileArray[returnFileArray.length]=startPath+tempFileArray[j];
  175.     }
  176.     return returnFileArray;
  177. }    
  178.  
  179. // -------------- Generic String Functions by Danny Goodman ---------------------- 
  180.  
  181. // extract front part of string prior to searchString
  182. function getFront(mainStr,searchStr){
  183.     foundOffset = mainStr.indexOf(searchStr);
  184.     if (foundOffset == -1) 
  185.         return null;
  186.     else
  187.         return mainStr.substring(0,foundOffset);
  188. }
  189.  
  190. // extract back end of string after searchString
  191. function getEnd(mainStr,searchStr) {
  192.     foundOffset = mainStr.indexOf(searchStr);
  193.     if (foundOffset == -1)
  194.         return null;
  195.     else
  196.         return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
  197. }
  198.  
  199. // -------------- Hal Pawluk string functions ---------------------- 
  200.  
  201.  
  202. function noSpaces(theString){
  203.     theEnd = theString.length;
  204.     for (i=0; i< theEnd; i++){
  205.         while ((theString.charAt(i)==" " || escape(theString.charAt(i))=="%0D") && i < theEnd){
  206.             theString = theString.substring(0,i) + theString.substring(i+1);
  207.             theEnd-- ;
  208.             }
  209.         }
  210.     return theString;
  211.     }
  212.  
  213. function subtractString(theString,theTakeaway){
  214.     theSplit = theString.indexOf(theTakeaway);
  215.     if (theSplit != -1);{
  216.         theLength = theTakeaway.length;
  217.         theString = theString.substring(0,theSplit) + theString.substring(theSplit+theLength);
  218.     }
  219.     return theString;
  220.     }
  221.     
  222. function insertInString(theString,theInsert,thePlace){
  223.     //to prepend use 'thePlace=0', to append use 'thePlace=theString.length'
  224.     if (thePlace<0 || thePlace>theString.length){
  225.         alert("Sorry, that's not in the string.");
  226.         return null;
  227.         }
  228.     else
  229.         return theString.substring(0,thePlace) + theInsert + theString.substring(thePlace);
  230.     }
  231.  
  232.  
  233.