コード例 #1
0
        static void Main(string[] args)
        {
            var phantomJS = new PhantomJS();

            // write result to stdout
            Console.WriteLine("Getting content from baidu.com directly to C# code...");
            var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bilibili.html");

            if (File.Exists(outFileHtml))
            {
                File.Delete(outFileHtml);
            }
            using (var outFs = new FileStream(outFileHtml, FileMode.OpenOrCreate, FileAccess.Write))
            {
                try
                {
                    phantomJS.RunScript(@"
						var system = require('system');
						var page = require('webpage').create();
						page.open('https://www.bilibili.com/', function() {
							system.stdout.writeLine(page.content);
                            page.render('cutPic.png');
							phantom.exit();
						});
					"                    , null, null, outFs);
                }
                finally
                {
                    phantomJS.Abort(); // ensure that phantomjs.exe is stopped
                }
            }
            Console.WriteLine("Result is saved into " + outFileHtml);
        }
コード例 #2
0
        public override void Init()
        {
            if (!(this.Importer is TwineHtmlImporter))
            {
                return;
            }

            // Run the story file in PhantomJS, inject the bridge script and deserialize the JSON output
            PhantomOutput <SugarStoryData> output;

            try
            {
                output = PhantomJS.Run <SugarStoryData>(
                    new System.Uri(Application.dataPath + "/../" + Importer.AssetPath).AbsoluteUri,
                    "sugar.bridge.js_"
                    );
            }
            catch (StoryImportException)
            {
                throw new StoryImportException("HTML or JavaScript errors encountered in the Sugar story. Does it load properly in a browser?");
            }

            // Add the passages to the importer
            this.Importer.Passages.AddRange(output.result.passages);

            // Add the start passage to the metadata
            this.Importer.Metadata.StartPassage = output.result.passages
                                                  .Where(p => p.Pid == output.result.startPid)
                                                  .Select(p => p.Name)
                                                  .FirstOrDefault();
        }
コード例 #3
0
        private static string ExtractHtmlWithPhantomJSNoWebdriver(string url)
        {
            Logger.Info($"Calling {url} with PhantomJS");

            string javascriptQuery = @"var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36';
page.onError = function(msg, trace) {
  //prevent js errors from showing in page.content
  return;
};
page.open('" + url + @"', function (status) {
    console.log(page.content); //page source
    phantom.exit();
});";

            string text = "";

            using (var outputStream = new MemoryStream())
                using (var phantom = new PhantomJS())
                {
                    phantom.CustomArgs = "--ssl-protocol=any --proxy-type=none";
                    phantom.RunScript(
                        javascriptQuery,
                        null,
                        null,
                        outputStream
                        );
                    text = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
                }

            Logger.Debug($"Response from {url}:\r\n{text}");
            return(text);
        }
コード例 #4
0
        /// <summary>
        /// Downloads PhantomJS package from nuget and unzips it if not exists.
        /// </summary>
        private void EnsurePhantomJsExe(PhantomJS phantomJs, string installPath)
        {
            var filePath = Path.Combine(phantomJs.ToolPath, phantomJs.PhantomJsExeName);

            if (!File.Exists(filePath))
            {
                lock (Sync)
                {
                    if (!File.Exists(filePath))
                    {
                        const string packageId      = "PhantomJS";
                        const string version        = "2.0.0";
                        var          repo           = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
                        var          packageManager = new PackageManager(repo, installPath);
                        try
                        {
                            Logger.Info($"downloading {packageId} version {version}");
                            packageManager.InstallPackage(packageId, SemanticVersion.Parse(version));
                            Logger.Info($"downloaded {packageId} version {version}");
                        }
                        catch (Exception ex)
                        {
                            Logger.Error($"failed to download {packageId} version {version}", ex);
                            throw;
                        }
                    }
                }
            }
        }
コード例 #5
0
        public override void Init()
        {
            // Run the story file in PhantomJS, inject the bridge script that invokes the Harlowe lexer and deserialize the JSON output
            PhantomOutput <HarloweStoryData> output;

            try
            {
                output = PhantomJS.Run <HarloweStoryData>(
                    new System.Uri(Application.dataPath + "/../" + Importer.AssetPath).AbsoluteUri,
                    Application.dataPath + "/Cradle/Editor/js/StoryFormats/Harlowe/harlowe.bridge.js_"
                    );
            }
            catch (StoryImportException)
            {
                throw new StoryImportException("HTML or JavaScript errors encountered in the Harlowe story. Does it load properly in a browser?");
            }

            // Add the passages to the importer
            this.Importer.Passages.AddRange(output.result.passages);

            // Add the start passage to the metadata
            this.Importer.Metadata.StartPassage = output.result.passages
                                                  .Where(p => p.Pid == output.result.startPid)
                                                  .Select(p => p.Name)
                                                  .FirstOrDefault();
        }
コード例 #6
0
ファイル: JaAgent2.cs プロジェクト: cupidshen/misc
        static JaAgent2()
        {
            _phantomJS = new PhantomJS();
            _phantomJS.OutputReceived += PhantomJSOutputReceived;
            _phantomJS.ErrorReceived += PhantomJSErrorReceived;

        }
コード例 #7
0
    private static void GetRenderedWebPage(string url, TimeSpan waitAfterPageLoad, Action <string> callBack)
    {
        const string cEndLine = "All output received";

        var sb = new StringBuilder();
        var p  = new PhantomJS();

        p.OutputReceived += (sender, e) =>
        {
            if (e.Data == cEndLine)
            {
                callBack(sb.ToString());
            }
            else
            {
                sb.AppendLine(e.Data);
            }
        };
        p.RunScript(@"
    var page = require('webpage').create();
    page.viewportSize = { width: 1920, height: 1080 };
    page.onLoadFinished = function(status) {
        if (status=='success') {
            setTimeout(function() {
                console.log(page.content);
                console.log('" + cEndLine + @"');
                phantom.exit();
            }," + waitAfterPageLoad.TotalMilliseconds + @");
        }
    };
    var url = '" + url + @"';
    page.open(url);", new string[0]);
    }
コード例 #8
0
        static void Main(string[] args)
        {
            var phantomJs = new PhantomJS();

            phantomJs.OutputReceived += (sender, e) => {
                Console.WriteLine($"PhantomJS output: {e.Data}");
            };
            phantomJs.RunScript("for (var i=0; i<10; i++) console.log('hello from js '+i); phantom.exit();", null);
        }
コード例 #9
0
        public PdfConverter()
        {
            phantomJs = new PhantomJS();

            phantomJs.OutputReceived += (sender, e) => {
                Count++;
                Console.WriteLine("PDF " + Count + " generated");
                Console.Write("Generating next.....");
            };
        }
コード例 #10
0
ファイル: Scraper.cs プロジェクト: pmcgriff/NCAA-Scraper
 public void RunScrap(string url)
 {
     // Create an instance of Phantom browser
     if (browser == null)
     {
         browser = new PhantomJS();
     }
     scrapResult = AquireText(url, javascriptCode);
     ProcessResult(url);
 }
コード例 #11
0
        public async Task <byte[]> CaptureByFile(string htmlPath, int width, int height, string format)
        {
            // phantomJs
            var workingFolder = EnsurePhantomJsWorkingFolder();
            var phantomJs     = new PhantomJS
            {
                ToolPath      = Path.Combine(workingFolder, @"PhantomJS.2.0.0\tools\phantomjs"), // combine with the nuget package path
                TempFilesPath = workingFolder
            };

            phantomJs.OutputReceived += (sender, args) =>
            {
                if (!string.IsNullOrEmpty(args.Data))
                {
                    // there is no output if generated successfully, using warn to log any error
                    Logger.Warn(args.Data);
                }
            };

            // process
            EnsurePhantomJsExe(phantomJs, workingFolder);
            var jsPath     = Path.Combine(phantomJs.ToolPath, @"examples\rasterize.js");
            var outputPath = Path.Combine(workingFolder, $"{Path.GetFileNameWithoutExtension(htmlPath)}-{Guid.NewGuid().ToString("N")}.{format}");

            try
            {
                await phantomJs.RunAsync(jsPath, new[]
                {
                    new Uri(htmlPath).AbsoluteUri, // note: using file:///xxx.html
                    outputPath,
                    $"{width}px*{height}px"
                });

                // result
                return(File.Exists(outputPath) ? File.ReadAllBytes(outputPath) : null);
            }
            finally
            {
                // clean up output
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }
            }
        }
コード例 #12
0
        void GetAmazonImageUrls(String url)
        {
            var result    = new ArrayList();
            var phantomJS = new PhantomJS();

            phantomJS.OutputReceived += (sender, e) => {
                if (e.Data != null)
                {
                    if (e.Data.StartsWith("result__"))
                    {
                        AddToTextArea(e.Data.Replace("result__", ""));
                    }
                    Console.WriteLine("PhantomJS output: {0}", e.Data);
                }
            };
            phantomJS.ErrorReceived += (sender, e) => {
                Console.WriteLine("PhantomJS error: {0}", e.Data);
            };

            var scriptString = @"
var webPage = require('webpage');
var system = require('system');
var args = system.args;

var page = webPage.create();

page.onConsoleMessage = function(msg) {
  console.log(msg);
}

page.open('" + url + @"', function(status) {
    var url= page.evaluate(function() {
        return document.getElementsByClassName('itemNo0')[0].getElementsByTagName('img')[0].src
    });
    console.log('result__' + url);
    phantom.exit();
});
";

            resultLabel.Text = "取得中";
            phantomJS.RunScript(scriptString, null);
        }
コード例 #13
0
        public static void RunPaymentScript(StartPaymentOperationResult result)
        {
            var phantomJS = new PhantomJS();

            phantomJS.OutputReceived += (sender, e) =>
            {
                if (e.Data != null)
                {
                    throw new Exception($"Payment script failed: {e.Data}");
                }
            };

            using (var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BarionClientLibrary.IntegrationTests.PaymentScript.js"))
                using (var streamReader = new StreamReader(fileStream))
                {
                    var paymentScript = streamReader.ReadToEnd();

                    phantomJS.RunScript(paymentScript, new[] { result.GatewayUrl, AppSettings.BarionPayer, AppSettings.BarionPayerPassword });
                }
        }
コード例 #14
0
        /// <summary>
        /// Export of the reports to PDF
        /// </summary>
        /// <param name="url">Link to the report</param>
        /// <param name="orientation">Landscape or Portrait orientation</param>
        /// <returns>Link to the generated pdf file</returns>
        public static string ExportReportPdf(string url, string orientation)
        {
            var phantomJs = new PhantomJS();

            string webUri = ConfigurationManager.AppSettings.Get("WebURI");

            const string fileName   = "";
            const string filePath   = "../Img/ReportsPdf/" + fileName;
            string       serverPath = HttpContext.Current.Server.MapPath("~/Img/ReportsPdf/") + fileName;

            phantomJs.RunScript(@"
                var page = require('webpage').create();
                page.viewportSize = { width: 1754, height: 1240};
                page.settings.resourceTimeout = 20000;
                page.paperSize = {
                    format: 'A4',
                    orientation: '" + orientation + @"',
                    margin: '1cm' 
                };

                page.open('" + webUri + "Account/Login" + @"', function() {
	                page.open('"     + url + @"', function() {
		                setInterval(function(){			
			                var title = page.evaluate(function() {				
				                $('nav').hide();				
			                });
			                page.render('"             + filePath + @"');
			
			                phantom.exit();
		                }, 5000);   
	                });	
                });",
                                null);

            if (File.Exists(serverPath))
            {
                return("Img/ReportsPdf/" + fileName);
            }

            return("");
        }
コード例 #15
0
        public string Scrap(string pageUrl)
        {
            var phantomJS = new PhantomJS();

            phantomJS.OutputReceived += (sender, e) =>
            {
                Console.WriteLine("PhantomJS output: {0}", e.Data);
            };
            //phantomJS.RunScript("'use strict'; document.write('Hello, world!'); phantom.exit();", null);
            List <string> s = new List <string>();

            s.Add(pageUrl);

            phantomJS.Run(Server.MapPath("Phantom.js"), s.ToArray());


            //phantomJS.RunScript(" var page = require('webpage').create(); page.viewportsize = {  width: 2000,  height: 700}; page.open('http://www.scratcharge.com/', function(status) { console.log('status: ' + status);  if(status === 'success') {    page.render('img/test88.png');  }  phantom.exit();});", null);
            //phantomjs.run("/hello.js", null);
            //phantomJS.RunScript("var webpage = require('webpage');var page = webpage.create();page.open('http://www.hpsmehra.info', function (status) {  var content = page.content;  console.log('content: ' + content);  phantom.exit();});", null);
            //phantomJS.RunScript("var webpage = require('webpage');var page = webpage.create();page.open('http://www.hpsmehra.info', function (status) {  var content = page.content;  console.log('content: ' + content); var fs = require('fs');var path = 'content/output.txt'; var data = content; fs.write(path, data, 'w'); phantom.exit();});", null);
            return(null);
        }
コード例 #16
0
        /// <summary>
        /// 加载js动态内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string GetJSContent(string url, string encode)
        {
            string text = "";

            var phantomJS = new PhantomJS();

            // write result to stdout
            //Console.WriteLine("Getting content from baidu.com directly to C# code...");
            //var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "baidu.html");
            //if (File.Exists(outFileHtml))
            //    File.Delete(outFileHtml);
            using (var outFs = new MemoryStream())
            {
                try
                {
                    phantomJS.RunScript(string.Format(@"
                        var system = require('system');
                        var page = require('webpage').create();
                        page.open('{0}', function() {{
                            system.stdout.writeLine(page.content);
                            phantom.exit();
                        }});
                    ", url), null, null, outFs);

                    byte[] b = outFs.ToArray();
                    text = System.Text.Encoding.GetEncoding(encode).GetString(b, 0, b.Length);
                }
                finally
                {
                    phantomJS.Abort(); // ensure that phantomjs.exe is stopped
                }
            }
            //Console.WriteLine("Result is saved into " + outFileHtml);

            return(text);
        }
コード例 #17
0
 public BaseSite()
 {
     this.phantomJS = new PhantomJS();
 }
コード例 #18
0
        public static string ExportSvfMap(int tip)
        {
            var    phantomJs = new PhantomJS();
            string webUri    = "http://*****:*****@"
                var page = require('webpage').create();
                page.viewportSize = { width: " + pdfViewportWidth + @", height: " + pdfViewportHeight + @"};       
                page.settings.resourceTimeout = 60000;
                page.paperSize = {
                    format: '" + paperFormat + @"',
                    margin: '1cm',
                    orientation: '" + pageOrientation + @"',
                    width: '" + pdfViewportWidth + @"px',
                    height: '" + pdfViewportHeight + @"px', 
                };
                
	                page.open('"     + theUrl + @"', function() {
		                setInterval(function(){
			                page.render('"             + filePath + @"');			
			                phantom.exit();
		                }, 20000);
	                });	
                ",
                                null);

            if (File.Exists(serverPath))
            {
                return(fileName);
            }

            return("");
        }
コード例 #19
0
ファイル: Program.cs プロジェクト: chami007/c--htmltopdf
        static void Main(string[] args)
        {
            var json = new JavaScriptSerializer();

            var phantomJS = new PhantomJS();

            phantomJS.OutputReceived += (sender, e) => {
                Console.WriteLine("PhantomJS output: {0}", e.Data);
            };
            phantomJS.ErrorReceived += (sender, e) => {
                Console.WriteLine("PhantomJS error: {0}", e.Data);
            };
            // provide custom input data to js code
            var inputData = json.Serialize(new[] {
                new User()
                {
                    name = "Bob", age = 30, company = "AirBNB"
                },
                new User()
                {
                    name = "Alice", age = 27, company = "Yahoo"
                },
                new User()
                {
                    name = "Tom", age = 31, company = "Microsoft"
                }
            });
            var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputData + "\n"));

            try
            {
                phantomJS.RunScript(@"
					var system = require('system');
					console.log('read data...');
					var inputData = system.stdin.readLine();
					console.log('done');
					var input = JSON.parse(inputData);
					for (var i=0; i<input.length; i++) {
						console.log('Name: '+input[i].name+'  Age: '+input[i].age);
					}
					phantom.exit();
				"                , null, inputStream, null);
            }
            finally
            {
                phantomJS.Abort(); // ensure that phantomjs.exe is stopped
            }

            Console.WriteLine();
            Console.WriteLine();

            // write result to stdout
            Console.WriteLine("Getting content from google.com directly to C# code...");
            var outFileHtml = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "google.html");

            if (File.Exists(outFileHtml))
            {
                File.Delete(outFileHtml);
            }
            using (var outFs = new FileStream(outFileHtml, FileMode.OpenOrCreate, FileAccess.Write))
            {
                try
                {
                    phantomJS.RunScript(@"
						var system = require('system');
						var page = require('webpage').create();
						page.open('https://google.com/', function() {
							system.stdout.writeLine(page.content);
							phantom.exit();
						});
					"                    , null, null, outFs);
                }
                finally
                {
                    phantomJS.Abort(); // ensure that phantomjs.exe is stopped
                }
            }
            Console.WriteLine("Result is saved into " + outFileHtml);

            Console.WriteLine();
            Console.WriteLine();

            // execute rasterize.js
            var outFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "google.pdf");

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }
            Console.WriteLine("Getting screenshot of google.com page...");
            try
            {
                phantomJS.Run(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rasterize.js"),
                              new[] { "http://nvd3.org/examples/discreteBar.html", outFile });
            }
            finally
            {
                phantomJS.Abort();
            }
            Console.WriteLine("Result is saved into " + outFile);
        }
コード例 #20
0
ファイル: HomeController.cs プロジェクト: RammohanKareti/Pocs
        private void ReadResponseHeaders(string url)
        {
            var scriptFile = Server.MapPath("~/Scripts/PhantomJS/ReadResponseHeaders.js");
            var scriptSource = System.IO.File.ReadAllText(scriptFile);

            var phantomJs = new PhantomJS();

            phantomJs.OutputReceived += (s, e) =>
                {
                    var response = e.Data;

                };

            phantomJs.RunScript(@"var system = require('system');
            var page = require('webpage').create();

            page.onResourceReceived = function (response) {
            console.log('Response (#' + response.id + ', stage ""' + response.stage + '""): ' + JSON.stringify(response));
            };

            page.open(system.args[1]);", new string[] { url});
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: hexiaohe/CommonTest
        public void Test(int i)
        {
            var phantomJS = new PhantomJS();
            //phantomJS.OutputReceived += (sender, e) =>
            //{
            //    Console.WriteLine("Got data from PhantomJS output: {0}", e.Data);
            //};
            var resUrl = string.Format("http://{0}/Home/InvitationRoute?p={1}", "xiaobao1001.cn", HttpUtility.UrlEncode(string.Format("oldinvitenew?id={0}&iscutpage=true", 615)));
            //http://xiaobao1001.cn/Home/InvitationRoute?p=oldinvitenew?id=12418&iscutpage=true
            using (var outMs = new MemoryStream())
            {
                try
                {
                    var temp = getSS();
                    phantomJS.RunScript(temp, new string[]
                    {
                        resUrl,
                        "640*1047"
                    }, null, outMs);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    phantomJS.Abort();
                }

                var buffer = outMs.ToArray();
                var imageInfo = Encoding.UTF8.GetString(buffer);
                if (!string.IsNullOrEmpty(imageInfo))
                {
                    buffer = Convert.FromBase64String(imageInfo);
                    var ms = new MemoryStream();
                    ms.Write(buffer, 0, buffer.Length);
                    var bitMap = new Bitmap(ms);
                    var path = @"D:\work\He\CommonTest\PhantomJsConsole\bin\Debug\" + Guid.NewGuid();
                    bitMap.Save(path + ".jpg");
                    bitMap.Dispose();
                    ms.Close();
                }
            }
        }