Best JavaScript code snippet using wpt
jasny-bootstrap.js
Source:jasny-bootstrap.js  
1/* ===========================================================2 * Bootstrap: fileinput.js v3.1.33 * http://jasny.github.com/bootstrap/javascript/#fileinput4 * ===========================================================5 * Copyright 2012-2014 Arnold Daniels6 *7 * Licensed under the Apache License, Version 2.0 (the "License")8 * you may not use this file except in compliance with the License.9 * You may obtain a copy of the License at10 *11 * http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing, software14 * distributed under the License is distributed on an "AS IS" BASIS,15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16 * See the License for the specific language governing permissions and17 * limitations under the License.18 * ========================================================== */19+function ($) { "use strict";20  var isIE = window.navigator.appName == 'Microsoft Internet Explorer'21  // FILEUPLOAD PUBLIC CLASS DEFINITION22  // =================================23  var Fileinput = function (element, options) {24    this.$element = $(element)25    26    this.$input = this.$element.find(':file')27    if (this.$input.length === 0) return28    this.name = this.$input.attr('name') || options.name29    this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]')30    if (this.$hidden.length === 0) {31      this.$hidden = $('<input type="hidden">').insertBefore(this.$input)32    }33    this.$preview = this.$element.find('.fileinput-preview')34    var height = this.$preview.css('height')35    if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {36      this.$preview.css('line-height', height)37    }38        39    this.original = {40      exists: this.$element.hasClass('fileinput-exists'),41      preview: this.$preview.html(),42      hiddenVal: this.$hidden.val()43    }44    45    this.listen()46  }47  48  Fileinput.prototype.listen = function() {49    this.$input.on('change.bs.fileinput', $.proxy(this.change, this))50    $(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))51    52    this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))53    this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))54  },55  Fileinput.prototype.change = function(e) {56    var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files57    58    e.stopPropagation()59    if (files.length === 0) {60      this.clear()61      return62    }63    this.$hidden.val('')64    this.$hidden.attr('name', '')65    this.$input.attr('name', this.name)66    var file = files[0]67    if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match(/^image\/(gif|png|jpeg)$/) : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {68      var reader = new FileReader()69      var preview = this.$preview70      var element = this.$element71      reader.onload = function(re) {72        var $img = $('<img>')73        $img[0].src = re.target.result74        files[0].result = re.target.result75        76        element.find('.fileinput-filename').text(file.name)77        78        // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account79        if (preview.css('max-height') != 'none') $img.css('max-height', parseInt(preview.css('max-height'), 10) - parseInt(preview.css('padding-top'), 10) - parseInt(preview.css('padding-bottom'), 10)  - parseInt(preview.css('border-top'), 10) - parseInt(preview.css('border-bottom'), 10))80        81        preview.html($img)82        element.addClass('fileinput-exists').removeClass('fileinput-new')83        element.trigger('change.bs.fileinput', files)84      }85      reader.readAsDataURL(file)86    } else {87      this.$element.find('.fileinput-filename').text(file.name)88      this.$preview.text(file.name)89      90      this.$element.addClass('fileinput-exists').removeClass('fileinput-new')91      92      this.$element.trigger('change.bs.fileinput')93    }94  },95  Fileinput.prototype.clear = function(e) {96    if (e) e.preventDefault()97    98    this.$hidden.val('')99    this.$hidden.attr('name', this.name)100    this.$input.attr('name', '')101    //ie8+ doesn't support changing the value of input with type=file so clone instead102    if (isIE) { 103      var inputClone = this.$input.clone(true);104      this.$input.after(inputClone);105      this.$input.remove();106      this.$input = inputClone;107    } else {108      this.$input.val('')109    }110    this.$preview.html('')111    this.$element.find('.fileinput-filename').text('')112    this.$element.addClass('fileinput-new').removeClass('fileinput-exists')113    114    if (e !== undefined) {115      this.$input.trigger('change')116      this.$element.trigger('clear.bs.fileinput')117    }118  },119  Fileinput.prototype.reset = function() {120    this.clear()121    this.$hidden.val(this.original.hiddenVal)122    this.$preview.html(this.original.preview)123    this.$element.find('.fileinput-filename').text('')124    if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')125     else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')126    127    this.$element.trigger('reset.bs.fileinput')128  },129  Fileinput.prototype.trigger = function(e) {130    this.$input.trigger('click')131    e.preventDefault()132  }133  134  // FILEUPLOAD PLUGIN DEFINITION135  // ===========================136  var old = $.fn.fileinput137  138  $.fn.fileinput = function (options) {139    return this.each(function () {140      var $this = $(this),141          data = $this.data('bs.fileinput')142      if (!data) $this.data('bs.fileinput', (data = new Fileinput(this, options)))143      if (typeof options == 'string') data[options]()144    })145  }146  $.fn.fileinput.Constructor = Fileinput147  // FILEINPUT NO CONFLICT148  // ====================149  $.fn.fileinput.noConflict = function () {150    $.fn.fileinput = old151    return this152  }153  // FILEUPLOAD DATA-API154  // ==================155  $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {156    var $this = $(this)157    if ($this.data('bs.fileinput')) return158    $this.fileinput($this.data())159      160    var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');161    if ($target.length > 0) {162      e.preventDefault()163      $target.trigger('click.bs.fileinput')164    }165  })...bootstrap-fileinput.js
Source:bootstrap-fileinput.js  
1/* ===========================================================2 * Bootstrap: fileinput.js v3.1.33 * http://jasny.github.com/bootstrap/javascript/#fileinput4 * ===========================================================5 * Copyright 2012-2014 Arnold Daniels6 *7 * Licensed under the Apache License, Version 2.0 (the "License")8 * you may not use this file except in compliance with the License.9 * You may obtain a copy of the License at10 *11 * http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing, software14 * distributed under the License is distributed on an "AS IS" BASIS,15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16 * See the License for the specific language governing permissions and17 * limitations under the License.18 * ========================================================== */19+function ($) { "use strict";20  var isIE = window.navigator.appName == 'Microsoft Internet Explorer'21  // FILEUPLOAD PUBLIC CLASS DEFINITION22  // =================================23  var Fileinput = function (element, options) {24    this.$element = $(element)25    26    this.$input = this.$element.find(':file')27    if (this.$input.length === 0) return28    this.name = this.$input.attr('name') || options.name29    this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]')30    if (this.$hidden.length === 0) {31      this.$hidden = $('<input type="hidden">').insertBefore(this.$input)32    }33    this.$preview = this.$element.find('.fileinput-preview')34    var height = this.$preview.css('height')35    if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {36      this.$preview.css('line-height', height)37    }38        39    this.original = {40      exists: this.$element.hasClass('fileinput-exists'),41      preview: this.$preview.html(),42      hiddenVal: this.$hidden.val()43    }44    45    this.listen()46  }47  48  Fileinput.prototype.listen = function() {49    this.$input.on('change.bs.fileinput', $.proxy(this.change, this))50    $(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))51    52    this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))53    this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))54  },55  Fileinput.prototype.change = function(e) {56    var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files57    58    e.stopPropagation()59    if (files.length === 0) {60      this.clear()61      return62    }63    this.$hidden.val('')64    this.$hidden.attr('name', '')65    this.$input.attr('name', this.name)66    var file = files[0]67    if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match(/^image\/(gif|png|jpeg)$/) : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {68      var reader = new FileReader()69      var preview = this.$preview70      var element = this.$element71      reader.onload = function(re) {72        var $img = $('<img>')73        $img[0].src = re.target.result74        files[0].result = re.target.result75        76        element.find('.fileinput-filename').text(file.name)77        78        // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account79        if (preview.css('max-height') != 'none') $img.css('max-height', parseInt(preview.css('max-height'), 10) - parseInt(preview.css('padding-top'), 10) - parseInt(preview.css('padding-bottom'), 10)  - parseInt(preview.css('border-top'), 10) - parseInt(preview.css('border-bottom'), 10))80        81        preview.html($img)82        element.addClass('fileinput-exists').removeClass('fileinput-new')83        element.trigger('change.bs.fileinput', files)84      }85      reader.readAsDataURL(file)86    } else {87      this.$element.find('.fileinput-filename').text(file.name)88      this.$preview.text(file.name)89      90      this.$element.addClass('fileinput-exists').removeClass('fileinput-new')91      92      this.$element.trigger('change.bs.fileinput')93    }94  },95  Fileinput.prototype.clear = function(e) {96    if (e) e.preventDefault()97    98    this.$hidden.val('')99    this.$hidden.attr('name', this.name)100    this.$input.attr('name', '')101    //ie8+ doesn't support changing the value of input with type=file so clone instead102    if (isIE) { 103      var inputClone = this.$input.clone(true);104      this.$input.after(inputClone);105      this.$input.remove();106      this.$input = inputClone;107    } else {108      this.$input.val('')109    }110    this.$preview.html('')111    this.$element.find('.fileinput-filename').text('')112    this.$element.addClass('fileinput-new').removeClass('fileinput-exists')113    114    if (e !== undefined) {115      this.$input.trigger('change')116      this.$element.trigger('clear.bs.fileinput')117    }118  },119  Fileinput.prototype.reset = function() {120    this.clear()121    this.$hidden.val(this.original.hiddenVal)122    this.$preview.html(this.original.preview)123    this.$element.find('.fileinput-filename').text('')124    if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')125     else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')126    127    this.$element.trigger('reset.bs.fileinput')128  },129  Fileinput.prototype.trigger = function(e) {130    this.$input.trigger('click')131    e.preventDefault()132  }133  134  // FILEUPLOAD PLUGIN DEFINITION135  // ===========================136  var old = $.fn.fileinput137  138  $.fn.fileinput = function (options) {139    return this.each(function () {140      var $this = $(this),141          data = $this.data('bs.fileinput')142      if (!data) $this.data('bs.fileinput', (data = new Fileinput(this, options)))143      if (typeof options == 'string') data[options]()144    })145  }146  $.fn.fileinput.Constructor = Fileinput147  // FILEINPUT NO CONFLICT148  // ====================149  $.fn.fileinput.noConflict = function () {150    $.fn.fileinput = old151    return this152  }153  // FILEUPLOAD DATA-API154  // ==================155  $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {156    var $this = $(this)157    if ($this.data('bs.fileinput')) return158    $this.fileinput($this.data())159      160    var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');161    if ($target.length > 0) {162      e.preventDefault()163      $target.trigger('click.bs.fileinput')164    }165  })...Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3  if (err) return console.error(err);4  console.log('Test status:', data.statusText);5  console.log('Test ID:', data.data.testId);6  console.log('Test results:', data.data.summary);7});8### new WebPageTest(host, [options])9* `protocol` - protocol to use for connecting to the WebPageTest server (default: `http`)10* `timeout` - timeout in milliseconds for requests to the WebPageTest server (default: `60000`)11### .runTest(url, [options], callback)12* `location` - location to run the test from (default: `Dulles:Chrome`)13* `connectivity` - connectivity profile to use (default: `Cable`)14* `firstViewOnly` - only run the test on the first view (default: `false`)15* `runs` - number of test runs to average the results from (default: `3`)16* `pollResults` - poll the test results until the test has completed (default: `true`)17* `pollInterval` - interval in milliseconds to poll the test results (default: `5000`)18* `timeout` - timeout in milliseconds to wait for the test to complete (default: `300000`)Using AI Code Generation
1const wptools = require('wptools')2const fs = require('fs')3wptools.page(url)4  .then(page => page.fileInput('image'))5  .then(image => {6    const file = fs.createWriteStream('image.jpg')7    image.pipe(file)8  })9  .catch(err => console.log(err))10const wptools = require('wptools')11const fs = require('fs')12wptools.page(url)13  .then(page => page.fileInput('image'))14  .then(image => {15    const file = fs.createWriteStream('image.jpg')16    image.pipe(file)17  })18  .catch(err => console.log(err))19const wptools = require('wptools')20const fs = require('fs')21wptools.page(url)22  .then(page => page.fileInput('image'))23  .then(image => {24    const file = fs.createWriteStream('image.jpg')25    image.pipe(file)26  })27  .catch(err => console.log(err))28const wptools = require('wptools')29const fs = require('fs')30wptools.page(url)31  .then(page => page.fileInput('image'))32  .then(image => {33    const file = fs.createWriteStream('image.jpg')34    image.pipe(file)35  })36  .catch(err => console.log(err))37const wptools = require('wptools')38const fs = require('fs')39wptools.page(url)40  .then(page => page.fileInput('image'))41  .then(image => {42    const file = fs.createWriteStream('image.jpg')43    image.pipe(file)44  })45  .catch(err => console.log(err))Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var fileInput = wptoolkit.fileInput;3var input = fileInput('file.txt');4input.pipe(process.stdout);5var wptoolkit = require('wptoolkit');6var fileInput = wptoolkit.fileInput;7var input = fileInput('file.txt');8var output = input.pipe(process.stdout);9var wptoolkit = require('wptoolkit');10var fileInput = wptoolkit.fileInput;11var input = fileInput('file.txt');12var output = input.pipe(process.stdout);13var wptoolkit = require('wptoolkit');14var fileInput = wptoolkit.fileInput;15var input = fileInput('file.txt');16var output = input.pipe(process.stdout);17var wptoolkit = require('wptoolkit');18var fileInput = wptoolkit.fileInput;19var input = fileInput('file.txt');20var output = input.pipe(process.stdout);21var wptoolkit = require('wptoolkit');22var fileInput = wptoolkit.fileInput;23var input = fileInput('file.txt');24var output = input.pipe(process.stdout);25var wptoolkit = require('wptoolkit');26var fileInput = wptoolkit.fileInput;27var input = fileInput('file.txt');28var output = input.pipe(process.stdout);Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.runTest(options, function(err, data) {6  if (err) return console.error(err);7  console.log(data);8});Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
