Retrieving data from twitter with JavaScript - javascript

I am using this js/jquery to scroll down a twitter page:
var f = function(i) { if(i < 999999999999) {
window.scrollTo(0,document.body.scrollHeight);
setTimeout(function(){f(i+1)}, 1000)
}
}
f(1)
And by doing it, I would like to save the webpage and do an scrape on it. But the problem is that my browser stop working, because the huge amount of info. So I want to know if there's any way to go save the page in the process.

Related

How to get string URL from multiple past pages JavaScript

I am very new to JavaScript. I am trying to make a web application, where a simple back button will go to a specific page I am looking for, one that has the word "search" in it. I don't know the exact URL, because the parameters within that URL change, and I need to keep it consistent to what the user wanted. But this one button should go back to that one page, regardless of the other links that were clicked.
For example:
If I clicked on
Home Page
Main Page
Page 1
Page 1.3
Back
I want the back to always take me to Main Page with the exact parameters it had before, not Home Page.
I tried the following:
The button itself
movieTableBodyElement.append('' + " << Back" + ''); // Building the HTML button, calls the goBackHelper() function
function goBackHelper()
{
// checks if the page directly behind is the Main Page with "search"
if(document.referrer.includes("search"))
{
// if its the main page, exit the function and end recursive call
window.history.go(-1);
}
else
{
// it is not the last page, so go to the past page and check again
window.history.go(-1);
goBackFunction();
}
}
But this takes me to the very first home page. I thought that document.referrer would get me the past URL, but it doesn't seem to be working for me. Is there a way to get the URL from past pages? So if I am on page 2, can I get all the URLs and search for Main Page? Any help is greatly appreciated!
I'm also new to Stack Overflow, so if there is any clarification please don't hesitate to let me know!
document.referrer is not the same as the actual URL in all situations.
Your best bet is to store the URLs in sessionStorage.
Add this snippet of code to your pages:
if (sessionStorage.getItem("locationHistory") !== null) {
var locationHistoryArray = JSON.parse(sessionStorage.getItem("locationHistory"));
locationHistoryArray.push(window.location.href);
sessionStorage.setItem("locationHistory", JSON.stringify(locationHistoryArray));
} else {
var locationHistoryArray = [];
locationHistoryArray.push(window.location.href);
sessionStorage.setItem("locationHistory", JSON.stringify(locationHistoryArray));
}
And this is your goBackHelper() function :
function goBackHelper() {
var searchString = 'search'; //modify this
var locationHistoryArray = JSON.parse(sessionStorage.getItem("locationHistory"));
for (i = 0; i < locationHistoryArray.length; i++) {
if (locationHistoryArray[i].includes(searchString)) {
window.location.assign(locationHistoryArray[i]);
break;
}
}
}
Read about document.referrer here.

Forward pages onto holding page then onto relevant page on new site

I have an old website that is being replaced (www.old.com) and a new website that's replacing it, and which has similar pages (www.new.com). I need all the pages within www.old.com to automatically redirect to a holding page (simple html page with the below countdown script and redirect function)... However, the challenge is that I then need the redirect to go to the relevant page on www.new.com.
code:
<script>
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count <= 0) {
window.location.href="https://example.com";
}
}, 1000);
</script>
For example... www.old.com/about/ shows the holding page and countdown timer, then forwards to www.new.com/about-us/.
To redirect only single page
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?
To redirect whole website permanently to other domain
Redirect 301 / http://www.new.com/
It's really important to implement a proper 301 redirect for any pages which change URL, so that the Google bot has an easy time understanding that the page has permanently moved location.
Google will then update it's index to point through specifically to the new page, and any inbound links and other signals will be attributed to the new page (usually allowing you to keep high rankings in Google search results provided all the content is still on the page, and that it's still properly optimised on-page (i.e. meta title tags, description, text copy on the page etc)).
NOTE :
This will not redirect email for your domain
Make a little modification to your Javascript code, such as
<script>
var old = window.location;
old = old.replace('http://www.old.com', '');
var newLocation = "https://example.com" + old;
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count <= 0) {
window.location.href = newLocation ;
}
}, 1000);
</script>
This will redirect your page as you expected.

How to retrieve results from a search from several pages

I am need to collect data from a website that allows me to download the results of a query but only the ones currently being displayed on that page.
I have no experience whatsoever with javascript or even any real programming. A friend told me that I might be able to do it with the "Custom JavaScript for websites" Addon for Chrome.
I managed to get it to download the file I want for each page using:
document.getElementById('dContent').value = 'full';
document.getElementById('submit-download').click();
I still need to manually change to the next page. I tried to do this automatically by adding a cycle:
for (i = 1; i < 20; i++) {
location.href='https://www.myurl.com/search?query=keyword&page=' +i;
document.getElementById('dContent').value = 'full';
document.getElementById('submit-download').click();
}
But it doesn't seem to work*. My google skills and limited knowledge only took me this far. Am I doing something wrong? I wonder if the addon actually allows this as I reckon it might not work as a new page is being loaded. Is there another software that I might use for this purpose?
Thank you in advance for your help.
the url for the queries follows the page=1 / =2 / ... rule for the results
setTimeout(function(){
document.getElementById('dContent').value = 'full';
document.getElementById('submit-download').click();
var page = parseInt(location.search.match(/\d\d?$/)[0])
if(!isNaN(page) && page < 20){
location.href='https://www.myurl.com/search?query=keyword&page=' +(page + 1);
}
}, 1000);

KDE plasmoid ind autorefresh

I'm trying to write KDE4 plasmoid in JavaScript, but have not success.
So, I need to get some data via HTTP and display it in Label. That's working well, but I need regular refresh (once in 10 seconds), it's not working.
My code:
inLabel = new Label();
var timer= new QTimer();
var job=0;
var fileContent="";
function onData(job, data){
if(data.length > 0){
var content = new String(data.valueOf());
fileContent += content;
}
}
function onFinished(job) {
inLabel.text=fileContent;
}
plasmoid.sizeChanged=function()
{
plasmoid.update();
}
timer.timeout.connect(getData);
timer.singleShot=false;
getData();
timer.start(10000);
function getData()
{
fileContent="";
job = plasmoid.getUrl("http://192.168.0.10/script.cgi");
job.data.connect(onData);
job.finished.connect(onFinished);
plasmoid.update();
}
It gets script once and does not refresh it after 10 seconds. Where is my mistake?
It is working just fine in here at least (running a recent build from git master), getData() is being called as expected. Can you see any errors in the console?
EDIT: The problem was that getUrl() explicitly sets NoReload for KIO::get() which causes it load data from cache instead of forcing a reload from the server. Solution was to add a query parameter to the URL in order to make it force reload it.

Auto refreshing with Javascript?

Sorry if this is a simple question (or answered before), but could anyone tell me how to download a page with JScript? I want to use Javascript to download the page "example.php" on my server every five seconds and compare it with what it was before. If the page changes, I would like the Javascript code to refresh the page hosting the Javascript. Does that make sense ? Somethingl ike:
string downloaded = DownloadPage("example.php");
timer x = new timer(5);
when the timer goes off:
if(DownloadPage("example.php") == downloaded){
RefreshPage();
}
Thanks, and sorry this was probably such an easy question :)
Using the jQuery framework's get function:
$.get('example', function(data) {
if(data == "something")
//perform action
});

Categories