PhantomJS - วิธีการ
PhantomJS เป็นแพลตฟอร์มที่ช่วยเรียกใช้งาน JavaScript โดยไม่ต้องใช้เบราว์เซอร์ ในการทำเช่นนั้นจะใช้วิธีการต่อไปนี้ซึ่งช่วยในการเพิ่มคุกกี้การลบการล้างการออกจากสคริปต์การแทรก JS และอื่น ๆ
เราจะพูดคุยเพิ่มเติมเกี่ยวกับวิธีการ PhantomJS และไวยากรณ์ของพวกเขาในบทนี้ วิธีการที่คล้ายกันเช่นaddcookie, injectjs มีอยู่ในโมดูลหน้าเว็บซึ่งจะกล่าวถึงในบทต่อ ๆ ไป
PhantomJS เปิดเผยวิธีการต่อไปนี้ที่สามารถช่วยให้เรารัน JavaScript ได้โดยไม่ต้องใช้เบราว์เซอร์ -
- addCookie
 - clearCookie
 - deleteCookie
 - Exit
 - InjectJS
 
ตอนนี้ให้เราเข้าใจวิธีการเหล่านี้โดยละเอียดพร้อมตัวอย่าง
addCookie
วิธี addcookie ใช้เพื่อเพิ่มคุกกี้และจัดเก็บในข้อมูล มันคล้ายกับวิธีที่เบราว์เซอร์จัดเก็บ ใช้อาร์กิวเมนต์เดียวที่เป็นวัตถุที่มีคุณสมบัติทั้งหมดของคุกกี้และรูปแบบดังที่แสดงด้านล่าง -
ไวยากรณ์
ไวยากรณ์มีดังนี้ -
phantom.addCookie ({ 
   "name" : "cookie_name",  
   "value" : "cookie_value", 
   "domain" : "localhost" 
}); 
    ชื่อค่าโดเมนเป็นคุณสมบัติบังคับที่ต้องเพิ่มในฟังก์ชัน addcookie หากไม่มีคุณสมบัตินี้ในวัตถุคุกกี้วิธีนี้จะล้มเหลว
name - ระบุชื่อคุกกี้
value - ระบุมูลค่าของคุกกี้ที่จะใช้
domain - โดเมนที่จะใช้คุกกี้
ตัวอย่าง
นี่คือตัวอย่างของไฟล์ addcookie วิธี.
var page = require('webpage').create(),url = 'http://localhost/tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      }); 
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length);  
      
      // will output the total cookies added to the url.    
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
}); 
    ตัวอย่าง
a.html
<html>
   <head>
      <title>Welcome to phantomjs test page</title>
   </head>
   
   <body>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
   </body>
</html> 
    โปรแกรมข้างต้นสร้างสิ่งต่อไปนี้ output.
Added 3 cookies 
Total cookies :3 
    ความคิดเห็นเกี่ยวกับรหัสสามารถอธิบายได้ด้วยตนเอง
clearCookies
วิธีนี้ช่วยให้สามารถลบคุกกี้ทั้งหมดได้
ไวยากรณ์
ไวยากรณ์มีดังนี้ -
phantom.clearCookies(); 
    แนวคิดนี้ทำงานคล้ายกับการลบคุกกี้ของเบราว์เซอร์โดยเลือกในเมนูเบราว์เซอร์
ตัวอย่าง
นี่คือตัวอย่างของไฟล์ clearCookies วิธี.
var page = require('webpage').create(),url = 'http://localhost/tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      }); 
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length); 
      phantom.clearCookies(); 
      console.log(
         'After clearcookies method total cookies :' +phantom.cookies.length); 
      
      phantom.exit();     
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
}); 
    a.html
<html>
   <head>
      <title>Welcome to phantomjs test page</title>
   </head>
   
   <body>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
   </body>
</html> 
    โปรแกรมข้างต้นสร้างสิ่งต่อไปนี้ output.
Added 3 cookies 
Total cookies :3 
After clearcookies method total cookies :0 
    deleteCookie
ลบคุกกี้ในไฟล์ CookieJarโดยมีคุณสมบัติ 'name' ตรงกับ cookieName มันจะกลับมาtrueหากลบสำเร็จ มิฉะนั้นfalse.
ไวยากรณ์
ไวยากรณ์มีดังนี้ -
phantom.deleteCookie(cookiename); 
    ให้เราเข้าใจ addcookie, clearcookies และ deletecookie ด้วยความช่วยเหลือของตัวอย่าง
ตัวอย่าง
นี่คือตัวอย่างเพื่อสาธิตการใช้เมธอด deleteCookie -
File: cookie.js
var page = require('webpage').create(),url = 'http://localhost/tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      });
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      });  
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length); 
      
      //will output the total cookies added to the url.    
      console.log("Deleting cookie2"); 
      phantom.deleteCookie('cookie2'); 
      
      console.log('Total cookies :'+phantom.cookies.length); 
      phantom.clearCookies();
      
      console.log(
         'After clearcookies method total cookies :' +phantom.cookies.length); 
      phantom.exit(); 
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
}); 
    โปรแกรมข้างต้นสร้างสิ่งต่อไปนี้ output.
phantomjs cookie.js
Added 3 cookies
Total cookies :3
Deleting cookie2
Total cookies :2
After clearcookies method total cookies :0 
    ออก
เมธอด phantom.exit จะออกจากสคริปต์ที่เริ่มต้น ออกจากโปรแกรมพร้อมค่าส่งคืนที่กล่าวถึง มันให้‘0’หากไม่มีค่าที่ส่งผ่าน
ไวยากรณ์
ไวยากรณ์มีดังนี้ -
phantom.exit(value); 
    ในกรณีที่คุณไม่ได้เพิ่ม phantom.exitจากนั้นบรรทัดคำสั่งจะถือว่าการดำเนินการยังคงเปิดอยู่และจะไม่เสร็จสมบูรณ์
ตัวอย่าง
ให้เราดูตัวอย่างเพื่อทำความเข้าใจการใช้ไฟล์ exit วิธี.
console.log('Welcome to phantomJs');  // outputs Welcome to phantomJS 
var a = 1; 
if (a === 1) { 
   console.log('Exit 1'); //outputs Exit 1 
   phantom.exit(); // Code exits. 
} else { 
   console.log('Exit 2'); 
   phantom.exit(1); 
} 
    โปรแกรมข้างต้นสร้างสิ่งต่อไปนี้ output.
phantomjs exit.js
Welcome to phantomJs 
Exit 1 
    ส่วนของโค้ดใด ๆ หลังจาก phantom.exit จะไม่ถูกเรียกใช้เนื่องจาก phantom.exit เป็นวิธีการสิ้นสุดสคริปต์
ฉีด Js
InjectJs ใช้เพื่อเพิ่ม addtionaljsไฟล์ใน phantom หากไม่พบไฟล์ในปัจจุบันdirectory librarypathจากนั้นคุณสมบัติ phantom (phantom.libraryPath) จะถูกใช้เป็นสถานที่เพิ่มเติมในการติดตามเส้นทาง มันกลับมาtrue หากการเพิ่มไฟล์สำเร็จเป็นอย่างอื่น false สำหรับความล้มเหลวในกรณีที่ไม่สามารถค้นหาไฟล์ได้
ไวยากรณ์
ไวยากรณ์มีดังนี้ -
phantom.injectJs(filename); 
    ตัวอย่าง
ให้เราดูตัวอย่างต่อไปนี้เพื่อทำความเข้าใจการใช้ injectJs.
Filename: inject.js
console.log(“Added file”); 
    File name: addfile.js
var addfile =  injectJs(inject.js);
console.log(addfile);
phantom.exit(); 
    เอาต์พุต
Command - C: \ phantomjs \ bin> phantomjs addfile.js
Added file // coming from inject.js
true 
    ในตัวอย่างข้างต้น addfile.js เรียกไฟล์ inject.jsโดยใช้ injectJs เมื่อคุณดำเนินการ addfile.js console.log ที่มีอยู่ใน inject.js จะแสดงในเอาต์พุต นอกจากนี้ยังแสดงเป็นจริงสำหรับตัวแปร addfile เนื่องจากเพิ่มไฟล์ inject.js เรียบร้อยแล้ว