Create a Tampermonkey script [duplicate] - javascript

This question already has answers here:
How to change the name field of an <input> in an AJAX-driven page?
(1 answer)
How to change a browsed page's <input>?
(1 answer)
Closed 4 years ago.
I want to create a Tampermoney script that writes something in an input field, if it finds a string on the page. E.g:
If the page contain the string "life is", the script should automatically append "good".
(I want to make this with word detection, so the string "life is", will appear on the page after that the page was accessed)

First you need to get the body as String and the input you want to modify
<input id='input-id' />
const page = document.body.innerHTML
const input = document.getElementById('input-id')
Then check if the value you want is in the page
if(page.search('life is') !== -1)
input.value = 'life is good'

Related

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

Store [value] contents from browser element as a string [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Get value of hidden input, send to another hidden input
(3 answers)
Closed 4 years ago.
I have a quick question regarding being able to store parts of an element as a string which I can later recall.
Example:
I have an element that looks like this
<input type= "hidden" name="Posted" id="Posted" value="100|307|151|16">
I can retrieve the element with document.getElementById('#Posted') and now I want to be able to take the contents in the [value] tag and store them as a string in a new variable, so I can get something like this:
var inputValue = "100|307|151|16"
Which I can later recall in my code.
Maybe I'm blind but after a bit of searching I've still come up with nothing. Thanks in advance for the help!
There's two different approaches you can take here.
You can either get the value of the hidden input, or you can get the value of the "value" attribute on the hidden input.
Here's how to do both:
var element = document.getElementById('Posted');
var value = element.value;
var attribute = element.getAttribute("value");
console.log(`value: ${value}`);
console.log(`attribute: ${attribute}`);
And here's a JSFiddle of that in action.
//If all you care about is the value, you can just use .value to get the information you want
const inputValue = document.getElementById('#Posted').value;
Have you tried:
var inputValue = document.getElementById('#Posted').getAttribute("value");

Simplest way to escape all special chars of user input text [duplicate]

This question already has answers here:
Sanitizing user input before adding it to the DOM in Javascript
(7 answers)
Closed 5 years ago.
I am adding a option to a select that is input by a user:
function prependToSelect(userInput){
$('#x').prepend('<option>' + userInput + '</option>');
}
what is the safest and most simple way to make sure there is nothing dangerous in userInput that can break the javascript or html? It would also be good if the solution/function would be reusable in different scenarios.
My inital answer would be to create a options element and set the innerText instead of innerHTML
but since u use jQuery...
$('#x').prepend($('<option>', {text: userInput}))
Don't add it as HTML.
document.querySelector("#x")
.appendChild(document.createElement("option"))
.textContent = userInput;
This adds it as .textContent so no HTML will be parsed, so no danger.

Clicking on an element with a variable attribute value in a certain format with CasperJS [duplicate]

This question already has an answer here:
Is it possible to use a CSS wildcard in the middle of an attribute selector?
(1 answer)
Closed 6 years ago.
I have that code snippet to click a button within CasperJS.
this.click('input[name="suggestion-to-repeat:5:suggestion:subForm:select-email"]');
The Problem is, there is an variable Number in the Name. In this example the "5". The Number can be between 1-10. Is there any chance to work with wildcards or any other possibilities to select the button?
Have a look to https://www.w3.org/TR/selectors/#selectors The following may help:
E[foo^="bar"]an E element whose "foo" attribute value begins exactly with the string "bar"
E[foo$="bar"]an E element whose "foo" attribute value ends exactly with the string "bar"
Try:
'input[Name^="suggestion-to-repeat:"][Name$=":suggestion:subForm:select-email"]'

JS: Append variable to URL without repeating [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change URL parameters with jQuery?
Currently, I have a <div> tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes or &view_all=No to the URL.
<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>
Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes or &view_all=No to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.
I have figured out a way to do the append but it keeps adding the &view_all variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No.
This is the way I am doing the append:
onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"
You can use regular expression to replace the value in a string:
$(this).attr("onclick", function(i, val) {
return val.replace(/view-all=(Yes|No)/, function() {
return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
});
});

Categories

Resources