open random pdf file from folder with coding (HTML main code used) - html

I have made this website where I collect recipes I can eat.
I would like to add a 'random' feature, where one of the recipes opens and I don't have to chose what to eat.
My knowlege of coding is limit (like a year of highschool making a HTML website limited) but I do now it has to be possible. I also learned very briefly about a random number generator option in PHP and Javascript.
The website is coded with HTML and Notepad++.
The files are all pdf typed and like this, '1.pdf' '2.pdf'

You mean something like
<script>
const max = 15; // the largest number in your PDF filename
const min = 1;
const rnd = Math.floor(Math.random() * (max - min + 1) + min);
const pdf = document.createElement("embed");
pdf.width="800px"; // or what you want
pdf.height="2100px";
pdf.src=rnd+".pdf"
window.addEventListener("load",function() {
document.body.appendChild(pdf);
});
</script>

Related

GAS: Can you use searchFiles on a searchFiles result?

This question might be more of a logical problem than a function problem.
I have two sets of PDFs "bought" and "return".
I use this to search for them:
qsBought = "fullText contains 'Bought' and mimeType='" + MimeType.PDF + "'";
qsReturn = "fullText contains 'Return' and mimeType='" + MimeType.PDF + "'";
Every file also have one device type in them. i.e. computer, chromebook, mac or iPad:
I can search for this with:
qsComputer = "fullText contains 'Computer' and mimeType='" + MimeType.PDF + "'";
I then use this to save the search result into a variable.
myFiles = parentFolder.searchFiles(qsXxx)
The result is then pushed to a sheet (that works like a I expect).
while(myFiles.hasNext()) {
var file, fileName, s, t;
file = myFiles.next();
fileName = file.getName();
s = fileName.substr(0, fileName.lastIndexOf('.')) || fileName;
t = s.split(' - ');
push(output, t, dv, qs);
}
output = colum headers
t = the name of the filename split
dv = supposed to be the device
qs = bought/returned
On every line I want to push out the information about if the devices is returned or bought.
I'm think that I can do a searchFiles(device) on the previous searchFiles(bought/returned) to find all computers bought, then computers returned, chromebooks bought and so on...
I've tried
qsBoughtComputer = "fullText contains 'Bought' and fullText contains 'computer' and mimeType='" + MimeType.PDF + "'";
I don't think searchFiles() support multiple fullText queries in the same search.
I don't fully grasp the logic or how to work with only these functions. If possible, I prefer to work with Googles core functions and repositories (first-party).
Thankful for any help in this!
As #doubleunary said in the comments.
Why would it not work?
I should've tried the solution after I made all the necessary changes to the code...
As I stated in the beginning;
This question might be more of a logical problem than a function
problem.

How to change background image randomly every load

How can I change the background image randomly from several sub folderseverytime the page is reloaded?
Imagine that the background file name is wallpaper.jpg
And in the folder images, I have subfolders and inside everyone of them I have one picture with the same name 'wallpaper.jpg'.
My goal is everytime we reload the page, the wallpaper changes randomly selecting one wallpaper.jpg from all the subfolders from the. Images folder.
Can you help me please?
Thanks
You will either have to make a naming pattern for the folders. Or put all the images in one folder with a naming pattern. I assume that your folder names are 0 1 2 3 4 5 6
<script type="text/javascript">
let folderCount= 5;
function refresh()
{
let rand= Math.ceil( Math.random() * folderCount);
document.body.background = images/'+num+'/wallpaper.jpg';
document.body.style.backgroundRepeat = "repeat";
}
</script>
You can do this simply with JavaScript like this:
let element = document.querySelector("body");
let arrImages = ["pathForImage1", "pathForImage2", "pathForImage3"];
let randomNumber = Math.floor(Math.random() * arrImages.length);
element.style.background = "url(" + arrImages[randomNumber] + ")";

Value Calculation issue in Google web HTML App

I have created an HTML web app in google script this works like a calculator, This app works fine if I add the input in descending order however if I skip the order and update in put data numbers randomly in any column then I am not getting the output properly
Example:- update the numbers in box number 4 and 5 then update in box number 1 you will find the differences in total numbers
Please refer the attached sheet for detailed script
Project Name- Project Proposal Form
$("#rTpe1").keyup(function(e){
$("#rFor1").val(this.value * $("#PerHourRate1").val());
$("#rFor3").val( Number($("#rFor1").val()) +Number($("#rFor2").val()))
});
$("#rTpe2").keyup(function(e){
$("#rFor2").val(this.value * $("#PerHourRate2").val());
$("#rFor3").val( Number($("#rFor1").val()) + Number($("#rFor2").val()))
});
$("#rTpe12").keyup(function(e){
$("#rFor12").val(this.value * $("#PerHourRate3").val());
$("#rFor3").val( Number($("#rFor1").val()) + Number($("#rFor2").val())+ Number($("#rFor12").val()))
});
$("#rTpe13").keyup(function(e){
$("#rFor13").val(this.value * $("#PerHourRate4").val());
$("#rFor3").val( Number($("#rFor1").val()) + Number($("#rFor2").val())+ Number($("#rFor12").val())+ Number($("#rFor13").val()))
});
I could be wrong, but I think that's the main culprit:
If your work your way top to bottom, the output in '#rFor3' is not affected. For example, if you enter values in the first field ('#rTpe1'), this statement
Number($("#rFor2").val()))
will evaluate to '0' because '#rFor2' probably contains an empty string at this point and Number("") will get you a zero. Because all subsequent input fields reference the results of previous calculations ('rTpe2' references 'rFor1', 'rTpe12' references both 'rFor1' and 'rFor2', etc), the sum will come out as correct.
Now consider the reverse scenario. For simplicity, let's make all your rates equal to 1. If you enter the value of '5' into 'rTpe12', the value of 'rFor3' will be
Number("") + Number("") + Number(5*1) == 5; //the first two inputs will contain empty strings at this point
The output of '#rFor3' would be 5. If you go up a step and enter the value of '2' into 'rTpe2', the value of the 'rFor3' output will change to
Number("") + Number(2*1) == 2; the first input will contain an empty string.
The code is not easy to understand, so even if this solution doesn't work for you, consider caching your DOM elements to improve performance and make your code more readable. Currently, you are using jQuery selectors to search the DOM over and over again, which is a serious performance drag. You could also store your calculated value as a variable and simply add values to it instead of recalculating on each input. For example
$('document').ready(function(){
var total = 0;
var input1 = $('#input1');
var input2 = $('#input1');
var input3 = $('#input1');
var output = $('#output');
input1.keyup(function(e){
var value = Number(this.value);
sum += value;
output.val(sum);
});
});

Simple Obfuscation Of String Constants in Flash

I am not a F
lash expert.
I have a FLA file of a game coded in ActionScript 3.
The game has a string inside, "www.mywebsite.com".
I want that when someone opens this FLA and searches for ".com" or "mywebsite.com" to find nothing. So I have decided to encode that string somehow. But I never coded in Flash, so I have no idea what to start with and Google isn't helping.
Basically all I want to do is replace this line:
var url1 = 'www.mywebsite.com';
With something like this and be functional.
var url1 = base64_decode('asdahwiyadwaeawr==');
Even a XOR or other simple string manipulation algorithm would be good.
What options do I have without importing any non-standard libraries into Flash?
Anyone looking through your code at something like var url = BlaBla_decode("cvxcvxc"); can simply replace it with var url = "www.HisWebsite.com...
So I guess you're supposing no one will be going through your script line by line but instead search for ".com" (Which would make him a really lazy jerk)!
A simple solution is to come up with a function that would return "www.MyWebsite.com" without writing it;
Something like:
var url:String = youAreStupid();
function youAreStupid():String
{
return String(f(22) + f(22) + f(22) + "extra.extra" + f(12) + f(24) + f(22) + f(4) + f(1) + f(18) + f(8) + f(19) + f(4) + "extra.extra" + f(2) + f(14) + f(12)).replace(/extra/g, "");
}
function f(n:Number):String
{
return String.fromCharCode("a".charCodeAt(0) + n);
}
I can't but say this would be lame way to protect your document, and I suggest you keep a comment at the top of your Script (something clearly visible) : // You won't find it YOU ARE STUPID
Now if he's smart enough to search for youAreStupid, that means he's entitled to change it :p
Of course there's also the simpler:
String("-Ow-Mw-Gw-!.-Ym-Oy-Uw-Ae-Rb-Es-Si-Ot-Se-T.-Uc-Po-Im-D").replace(/-./g, "");
but that's no fun!!!

How do I find the page number/number of pages in a document?

I want to create a new document based on a template and need to know when my insertion or append results in a new page in the final printed output is there any property/attribute eg number of pages that can be used for this?
I've search this a lot in the past and I don't think there's any property or any other way to know page info.
The solution I use is to insert page breaks on my template or via the script, using my own knowledge of how my template works, i.e. how much space it takes as I iterate, etc.
And then I know which page I am by counting the page breaks.
Anyway, you could an enhancement request on the issue tracker.
One way to get total number of pages:
function countPages() {
var blob = DocumentApp.getActiveDocument().getAs("application/pdf");
var data = blob.getDataAsString();
var re = /Pages\/Count (\d+)/g;
var match;
var pages = 0;
while(match = re.exec(data)) {
Logger.log("MATCH = " + match[1]);
var value = parseInt(match[1]);
if (value > pages) {
pages = value;
}
}
Logger.log("pages = " + pages);
return pages;
}