728x90
반응형

a_ict_word라는 클래스 이름으로 된 모든 span 요소를 제거하여, 문장 내에서 특정 단어를 강조하는 역할로 삽입된 요소를 제거하여, 끊김 없는 문장을 추출하는 프로그램입니다. 다소 규칙기반적인 사항으로 작성되었습니다.

 

if ( document.querySelectorAll('.a_ict_word') != undefined) {

        const spanElms =  document.querySelectorAll('.a_ict_word');

		if( spanElms.length > 0 ) {
			var parentIDArray = new Array( spanElms.length );
           	var parentID = 0;
        	parentIDArray[0] = parentID;
        	previousParent = spanElms[0].parentNode;
        	for( ii =1; ii<spanElms.length; ii++){
        		currentParent = spanElms[ii].parentNode;
        		parentIDArray[ii] = parentID;
        		if( currentParent != previousParent){
        			previousParent = currentParent;
        			parentIDArray[ii] = ++parentID;
        		}
        	}	
		}	

        jj=0;
        for( jj = 0; jj<spanElms.length; jj++){
            if( spanElms[jj].previousSibling != undefined && spanElms[jj].nextSibling != undefined && spanElms[jj].childNodes[0] != undefined ){
                 makeSentence = spanElms[jj].previousSibling.textContent.replace(/^\s+|\s+$/g,"")  + " "+ spanElms[jj].childNodes[0].textContent.replace(/^\s+|\s+$/g,"") + spanElms[jj].nextSibling.textContent.replace(/^\s+|\s+$/g,"");
                 spanElms[jj].previousSibling.textContent = makeSentence;
                 spanElms[jj].childNodes[0].textContent ="";
                 spanElms[jj].nextSibling.textContent="";
				
				if( spanElms[jj].nextSibling && spanElms[jj].nextSibling.nextSibling && spanElms[jj].nextSibling.nextSibling.textContent){
                      	if( spanElms[jj].nextSibling.nextSibling.textContent.indexOf('<br>') || spanElms[jj].nextSibling.nextSibling.textContent.indexOf('</p>' ) || spanElms[jj].nextSibling.nextSibling.textContent.indexOf("''" ) || spanElms[jj].nextSibling.nextSibling.textContent.length == 0 ){
                           		continue;
                       	} 
						if( (spanElms[jj].nextSibling.nextSibling.tagName) && (spanElms[jj].nextSibling.nextSibling.tagName == "BR") )
                           		continue;
						}

						if( spanElms[jj].nextSibling.nextSibling == undefined )
                                continue;
                                
                        for( kk=jj ; kk <spanElms.length; kk++ ){
								if( parentIDArray[ jj ] != parentIDArray[ jj + kk ] )
                                	break;

						if( spanElms[jj + kk ] && spanElms[jj + kk ].previousElementSibling ){
                           	    if( spanElms[jj + kk ].previousElementSibling.nodeName == 'BR' )
                        			break;	 		                                		 
                               	}

                                if( spanElms[jj + kk ] && spanElms[jj + kk ].nextElementSibling ) {
		                                if(  spanElms[jj + kk ].nextElementSibling.className === 'a_ict_word'){
		                                		 spanElms[jj ].previousSibling.textContent += spanElms[jj + kk ].nextElementSibling.childNodes[0].textContent;
		                                 		 spanElms[jj + kk ].nextElementSibling.childNodes[0].textContent="";
		                                 		 
		                                 		 spanElms[jj ].previousSibling.textContent += spanElms[jj + kk ].nextElementSibling.nextSibling.textContent;
		                                 		 spanElms[jj + kk ].nextElementSibling.nextSibling.textContent ="";       	 	
		                                }
	                              }
                        }
                              
              }
              else if(  spanElms[jj].previousSibling == undefined && spanElms[jj].nextSibling != undefined ){
                        makeSentence = spanElms[jj].childNodes[0].textContent.replace(/^\s+|\s+$/g,"") + spanElms[jj].nextSibling.textContent.replace(/^\s+|\s+$/g,"");
                        spanElms[jj].childNodes[0].textContent = "";
                        spanElms[jj].nextSibling.textContent=makeSentence;
              }
        }
 }
728x90
반응형

+ Recent posts