1const Apify = require('apify');
2const utils = require('apify/build/utils');
3const ApifyClient = require('apify-client');
4require('puppeteer');
5const path = require('path')
6require('jquery');
7const cheerio = require('cheerio')
8
9const humanDelay = ms => (Math.random() + 1) * ms;
10
11const sleepPromised = ms => new Promise(resolve => setTimeout(resolve, ms));
12
13const saveScreen = async (page, key) => {
14 const screenshotBuffer = await page.screenshot();
15 await Apify.setValue(key, screenshotBuffer, { contentType: 'image/png' });
16 const html = await page.evaluate('document.documentElement.outerHTML');
17 await Apify.setValue(key+'txt', html, { contentType: 'text/html' });
18};
19
20const opts = {
21 args: [
22 '--disable-web-security',
23 ],
24
25};
26
27var browser = {};
28
29const setCookies = async (page) => {
30
31}
32
33const pageGoto = async (page, url, timeout, retryCount) => {
34
35 let done = '';
36 let counter = 0;
37 await setCookies(page);
38 do {
39 try {
40
41
42 var response = await page.goto(url,{ waitUntil: 'networkidle2',timeout: timeout });
43
44 done = '';
45 }
46 catch (error){
47 console.log(error)
48 counter += 1;
49 done = error;
50 await page.close();
51 if (counter>1){
52 browser = await Apify.launchPuppeteer(opts);
53 console.log('new browser');
54 }
55 page = await browser.newPage();
56 await new Promise((resolve) => setTimeout(resolve, 3000));
57 console.log('RETRY Error')
58 }
59 if (response) {
60 if (response.status()!==200){
61 await page.close();
62 if (counter>1){
63 browser = await Apify.launchPuppeteer(opts);
64 console.log('new browser');
65 }
66 page = await browser.newPage();
67 counter += 1;
68 done = '403';
69 await new Promise((resolve) => setTimeout(resolve, 3000));
70 console.log('RETRY '+response.status())
71 }
72 }
73 } while (done!=='' && counter < retryCount)
74
75 if (done!==''){
76 console.log('RETURN NULL')
77 return null;
78 }
79 else
80 {
81 return page;
82 }
83}
84
85Apify.main(async () => {
86 var results = [];
87
88 const input = await Apify.getValue('INPUT')
89
90 var userName = input.userName;
91 var password = input.password;
92 var url = input.url;
93
94 console.log('Launching Puppeteer...');
95 browser = await Apify.launchPuppeteer(opts);
96 var page = await browser.newPage();
97
98 await page.setUserAgent('Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; MALCJS; rv:11.0) like Gecko');
99 page = await pageGoto(page,"https://YourGrafanaDomain.com/login", 100000, 10);
100 if (page!==null){
101 console.log('logIn');
102 await page.type('[name="username"]', userName, { delay: 100 });
103 await page.type('[id="inputPassword"]', password, { delay: 100 });
104 page.click('[type="submit"]');
105 await page.waitForNavigation({ waitUntil: 'networkidle2' });
106 console.log('after logIn submit');
107 }
108
109 page = await pageGoto(page,url, 100000, 10);
110
111 page.click('[bs-tooltip="\'Share dashboard\'"]');
112 try {
113 await page.waitForSelector('a[class="gf-tabs-link"]');
114 }
115 catch (error) {
116 await saveScreen(page,'errr');
117 throw error;
118 }
119
120 page.click('a[class="gf-tabs-link"]');
121
122 try {
123 await page.waitForSelector('[ng-click="createSnapshot()"]');
124 }
125 catch (error) {
126 await saveScreen(page,'errr');
127 throw error;
128 }
129
130 page.click('[ng-click="createSnapshot()"]');
131
132 try {
133 await page.waitForSelector('a[class="large share-modal-link"]');
134 }
135 catch (error) {
136 await saveScreen(page,'errr');
137 throw error;
138 }
139
140 let href = await page.$eval('a[class="large share-modal-link"]', a => a.href)
141
142 await Apify.setValue('OUTPUT', href);
143
144 console.log('Done.');
145});