Google Apps Script update properties with Trigger - javascript

I am creating a script to collect specific datas from Gmail to Google SpreadSheet. Because there are too many mails the execution time is over 6 minutes so i want to use triggers to automaticaly run the script every 6 minutes. My idea is too use Script Properties to store the ID of the last appended thread in order to get it back at the next script execution and keep gathering datas from other threads.
The problem is: when i manualy start the script the Properties are well updated but when the Trigger does it the Properties are not updated. Do you have any explanations ?

Well, never mind, i have found the problem, so for any one having the same one:
Don't try to test your script with an ui.alert when using a Trigger, it was firring an error and prevented the correct execution of the instructions after that.

Related

How to make setInterval work correctly in background tab

I'm using setInterval (within 3 Tampermonkey scripts) to check three different public websites every few seconds, so I can be alerted when specific text appears. These alerts are for freelance work offers, which can expire within seconds so I have to be quick.
It all works correctly, except when I'm working in a different tab or app, then that after about 6 minutes, setInterval starts to "trigger" for the background tab once per minute instead of once every few seconds.
Any suggestions how to fix this? Is it possible to use Date.now() in some way?
Note, I'm a complete beginner, willing to learn but need to keep things as simple as possible.
I've tried reloading the page every 3 minutes using window.location.reload() but that doesn't work. I guess I could create a script to activate and focus the tab every few minutes, but that would interrupt anything I was working on. I tested it with the following barebones script against https://www.google.co.uk/, in case something else in my script was causing a problem, but the same happens:
var i = 0;
setInterval(function() {
console.log("log: i:" + i);
i = i+1;
if(i==15) {
i = 0;
window.location.reload();
console.log("reloaded window");
}
}, 10000);
After a few minutes, i is incremented only once per minute - even following the window reload.
I've looked at this question
It mentions "workers" but can these be used within tampermonkey on public website I don't own? It also provides a link which suggests a workaround of playing an almost inaudible audio file - but I don't know if playing that within my tampermonkey script would work?
I see there are a number of workarounds here but I'm not sure if I can use any of them.
For example, can MutationObserver be used within a tampermonkey to detect changes in a public website? Even if it can, presumably I'd have to reload the webpage every time I needed to checK? Currently I'm using XMLHttpRequest instead of loading the webpage (far quicker and uses less CPU).
Interestingly, the above link seems to suggest that setInterval and SetTimeout are specifically targetted for throttling, I wonder if that means I could use some other function instead.
I've also seen this but I guess I can only use that for a website I own?
I can think of a few options.
Instead of having three scripts, use a single script, and run that single script on every site (with // #match *://*/*). Then, with that single script, set the interval. Whenever the interval callback runs, use Tampermonkey's GM_setValue and GM_getValue for cross-domain storage to coordinate executions - a callback will first check with getValue whether the last check was more than 3 minutes ago. If so, it calls setValue with the current date and performs the check. This way, even if the script is running on 100 different tabs (some in active tabs, some in background tabs), it'll still run the check once every few minutes.
To perform the check, use Tampermonkey's GM_xmlHttpRequest to get across same-origin restrictions; make a request to the three sites, and parse them into documents using DOMParser so you can programatically search through them for the elements you're looking for.
Perform the checks from a backend app instead of from a browser - for example, with Node and Puppeteer, which won't have throttling issues. To have the results be communicated with you, you could either have a userscript or websocket with your local webserver, or you could integrate the webserver into an Electron app instead. This is the approach I'd prefer, I think it's the most robust.
Use workers, which has worked for some

Chrome.storage.sync usage before page starts to add DOM elements

I am making an extension for university site, which i have no back-end access to. The task is to reorganize certain block elements, for which im trying to store information about in the string with numbers (elements are added into this string after the page loads). It worked well with localStorage, but ultimately there was a problem - each time the site admins update the site, the cache get cleared and the data is lost, so i choose to move to chrome.storage API.
Now, here is the problem. The behavior of chrome.storage.sync.get is rather incosistent, it might be slow enough to not get any data in the cache in time for MutationObserver to start working. I understand chrome.storage.sync.get is a asynchronous callback, but have no way to force it to work before the page starts getting filled.
window.toDelete = [];
chrome.storage.sync.get('course', function(entries){
window.toDelete = deserializeEntries(entries.course);
console.log("hello");
})
// Mutation observer declaration and .observe call
In this example it might go either way, "hello" being printed before or after MutationObserver does all the work. I have a call that prints the contents of toDelete in MutationObserver to tell if it works with properly filled cache or not. Example 1 Example 2

How to create a dynamically scheduled task?

I am trying to create an app that simulates opening a tab at a bar. I am running into one issue that I can't seem to figure out - it goes as follows:
When someone opens a bar tab, dynamically create a scheduled task that executes code to close the tab after 24 hours.
If the tab gets closed before the 24 hours, cancel the scheduled task.
If the tab doesn't get closed after 24 hours, execute the code described in step 1 to initiate a payment on the card used to open the tab.
I was initially looking into Firebase Functions, and was thinking about using a setTimeout() callable function, but after doing some research I found that Firebase Function's cannot be invoked for longer than 9 minutes.
NOTE: I would like this to be dynamic. Meaning, having it account for a variable amount of users. There could be 100 or 1000 users on the platform, each of them needs the ability to have a unique scheduled task for them (sometimes multiple per user).
Please see the comments for the full solution.
There are multiple approaches to circumvent the 10 minutes rule (which is prevalent in the serverless code) but here's something that can help you. I suggest separating the task into three:
A cloud function that close the tab when called.
A schedule function that calls it (https://firebase.google.com/docs/functions/schedule-functions)
A way to start and stop the schedule function.
I am not sure how firebase function work, but I worked with azure functions before and those can be controlled with command line (CLI) or with a sdk for your language of choice. To cancel using the command line, try something like this:
firebase functions:delete scheduledFunction
from How to cancel a scheduled firebase function?.
Now what's left is how to figure out how to start the function, and if it's possible to pass in a parameter to schedule it.
Good luck!

PHP altetrnative way how to timeout

i am making a tool where user can provide a delay time of response. It should be easy to do however when I used set_time_limit from doc
Warning: set_time_limit() has been disabled for security reasons
From error and by googling a bit it's obvious that hosting provider disable this functionality in PHP.
Question:
However I want to ask you if there is some alternative way how to timeout response using php without this function.
I can imagine this can be done via pass this timeout value to JS and timeout it via that but I hope if there isn't some other soluction for this.
The only idea I had is to put in your script some "waypoints" where you check the execution time, and if the execution time is too long terminate the script using die(), to check the execution time you can use new DateTime("now"); at every "waypoint" and then something like $interval = $datetime1->diff($datetime2); to get the interval between the first time you had and the last one

Queue .Net Code Behind Execution

I am working on a .Net application which has a blank .aspx page which links to a code-behind (.aspx.cs) sheet. It is called by JavaScript functions from many places which call the page with query-string parameters.
Dependent on the parameters - the page runs 2 families of stored procedures. One family creates records - the other updates records. Any SQL errors are then e-mailed over to me.
There is an error that I suspect is due to the code being executed twice in rapid succession - I receive a unique primary key constraint error. It seems that the page is being called a second time before the INSERT has taken place. This unfortunately is causing data-loss.
My question is...
How do I "Lock" or "Queue" aspx.cs page executions/requests to ensure that one "instance" or "thread" of the page completes before another may be fired?
Separate the invoking of the process from the execution of the process.
Basically you would add a table to the database as your "queue", each record of which has the information necessary to perform the process in question. (The data posted from the page, basically, as well as any other meta-data like the user who queued it and the time it was queued, etc.) The code on the page would do nothing more than simply insert a record in that table.
Separate from the ASP.NET application you would have a background process (a Windows Service might be best, a scheduled Console Application may work just as well) which periodically checks that table for new records. When it finds records, it executes the process in question on each one successively.
This allows the web application to still be multi-threaded (which you definitely want) while processing the records in a single thread.

Categories