Are you sure you want to go to an external site to donate a monetary value?
WARNING: Some countries laws may supersede the payment processors policy such as the GDPR and PayPal. While it is highly appreciated to donate, please check with your countries privacy and identity laws regarding privacy of information first. Use at your utmost discretion.
Hey guys, I'm very new here and was looking into a scripting option for what I need done. I know java and some JavaScript, but other than that I'm blank. All I'm trying to do with this script is get a value on a webpage and then save it somewhere - most likely on the users hard drive in a text file. It will also refresh the page and get the new loaded value every set interval of time (30 minutes - 1 hour). Can anyone help me out or point me in the right direction?
Re: @DevinJB:
You can use your browser's inspector (Inspect Element) feature to dive into the structure of the page and figure out how to extract the desired value. In particular, you will need to determine the most reliable selector (this could be a unique id attribute, or you may need to stack various tags and class names). https://developer.mozilla.org/docs/Web/Guide/API/DOM/Locating_DOM_elements_using_selectors
But... the second part is a problem. User scripts cannot write to the file system, just as web pages cannot write to the file system. If that is essential, you would need to write an add-on or separate program that has more privileged access to the system. If you are not limited to saving a file, you can stash data in local DOM storage, or in Greasemonkey preference data, or in a cloud (by posting to a server under your control).
First of all. you should know your page URL mask check what does your page look like, for now lets say it looks like:
so for future reference our include rule will look like this:
// @include http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit
Now lets see what can we do with value we have on a page. Lets say it's an input field with name "LastName" current value "Mouse". You need to read a bit about selectors, but it's not hard, you also could use a browser element inspector, it will help with. We have this
html body form input[name="LastName"]
selector.Lets check it with browser console (Use F12 hotkey to check it yourself on page http://www.w3schools.com/tags/tryhtml_form_submit.htm )
We need to get ".value" of this field so let's use
document.querySelector('html body form input[name="LastName"]').value
Now we have value, let's save it. Generally Userscripts use LocalStorage or GM_getValue/GM_setValue functions. We'll use GM_getValue/GM_setValue.
First we'll need to grant them both. It's Userscript MetaValue like this:
// @grant GM_getValue // @grant GM_setValue
Lets also forbid frames and add a rule to run script at documents end.
// @noframes // @run-at document-end
So our script will look like this:
// ==UserScript== // @author Dexmaster // @grant GM_getValue // @grant GM_setValue // @include http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit // @name Test Script Saver (GM_getValue/GM_setValue) // @namespace TestScript // @noframes // @run-at document-end // @version 0.1 // ==/UserScript== (function () { "use strict"; document.addEventListener("DOMContentLoaded", function() { var el = document.querySelector('html body form input[name="LastName"]'), vals = GM_getValue("my_vals", []); if (el !== undefined) { // If element exists if (vals.indexOf(el.value) < 0) { // And was not added vals.push(el.value); // ADD Value TO AN ARRAY GM_setValue("my_vals", vals); // Save Array } } }); }());
And another example using localStorage:
// ==UserScript== // @author Dexmaster // @grant none // @include http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_submit // @name Test Script Saver (localStorage) // @namespace TestScript // @noframes // @run-at document-end // @version 0.1 // ==/UserScript== (function () { "use strict"; document.addEventListener("DOMContentLoaded", function () { var el = document.querySelector('html body form input[name="LastName"]'), my_vals = []; if (el !== undefined) { // If element exists if ((localStorage.my_vals === null) || (localStorage.my_vals === undefined) || (typeof localStorage.my_vals !== "string") || (localStorage.my_vals === "")) { my_vals[0] = el; // FIRST VAL IN ARRAY )) } else { my_vals = JSON.parse(localStorage.my_vals); if (my_vals.indexOf(el) < 0) { my_vals.push(el); // ADD VAL TO ARRAY } } if (typeof my_vals === "object") { localStorage.my_vals = JSON.stringify(my_vals); // SAVE VALS ARRAY TO STORAGE } } }); }());
Also you'll probably need to reload page on timeout
setTimeout(function(){ window.location.reload(); /* or window.location = window.location.href; */ }, (int)( 1000 * 60 * ( 30 + ( Math.random() * 30 ) ) ) ); // (1sec * 60 * (30 + 0~30)) 30~60 minutes
Now to get out a value you'll just need to output into console
GM_getValue("my_vals")
orlocalStorage.my_vals
.This will look like this.
console.log( localStorage.my_vals );
or
console.log( GM_getValue( "my_vals" ) );
That's All Folks!
Final versions:
Test Script Saver (GM_getValue/GM_setValue)
or
Test Script Saver (localStorage)