I want to take url from page's view page source - javascript

For example this url
https://in.pinterest.com/pin/695524736192250687/
from its page source want to take
<img alt=" " class="hCL kVc L4E MIw" importance="auto" loading="auto" src="https://i.pinimg.com/236x/a8/7f/22/a87f2200109b01fc7a74b6106cb76f7b.jpg"/>
echo this from its page source:
https://i.pinimg.com/236x/a8/7f/22/a87f2200109b01fc7a74b6106cb76f7b.jpg
on
https://youtubethumbnaildownload.online

You can use SimpleHtmlDom to scrape the data from the source code and then look for the class and img tags or whatever else you need to do.
A simple PHP HTML DOM parser written in PHP5+, supports invalid HTML, and provides a very easy way to find, extract and modify the HTML elements of the dom. jquery like syntax allow sophisticated finding methods for locating the elements you care about.

You can store this url into session variable for the current page and then retrieve this session on another page where you want that url.
like :
$_SESSION['url'] = $url; // store value/url in session
$uel= $_SESSION['url']; //retrieve session value on another page
Make sure that you need to strat your session first.
session_start();

Related

Insert HTML Content from URL into HTML (form, div, iframe, ...)

I want to display the result of this URL
http://api.openweathermap.org/data/2.5/weather?q=Berlin,de&mode=html&appid=...1
which looks like
https://i.stack.imgur.com/WWRoD.png
how can I insert the HTML content from the URL directly into an iframe or div?
Thanks!
If you already are able to get that response html, all you need to do is insert it onto the page
document.getElementById('mydiv').innerHTML = "<p>some html</p>"
This could easily be achieved by PHP.
<?php
echo file_get_contents("ENTER URL HERE");
?>
You could do this via JavaScript, but that would require making an AJAX request to get the HTML and then inserting it into the DOM. I don't think that this would be the best method as the page would have already loaded without the code and then asynchronously adding it to the page. Depending on how it is meant to be viewed, I think this would lead to a poorer UX.
Edit
To download the HTML asynchronously, you should use .get(), instead of .load().
$.get("URL", function(data) {
$(".mydiv").html(data);
});

Check if an element on a page is not hidden from real users?

Using PHP/Javascript, is it possible to check whether an element (let's say a link) exists AND is actually VISIBLE by a real person on a remote website?
I know it's possible to check if a link/element exists in a source of a page (via using cURL or file_get_contents() function), but it may happen an element is hidden behind a <style="display:none">{element}</style> CSS style or class or between <script> or comment tags - then it won't show up for a public user.
So I wanted to check if it's possible to find out if an element is visible in a source code, but not visible to an actual/real user. It's probably impossible but wanted to make sure..
I see 2 options:
using javascript (and jQuery) to check for visility and hidden tags
see: https://api.jquery.com/visible-selector/ and https://api.jquery.com/hidden-selector/
Recreate the page in a DOMDocument and iterate the nodes to check for attributes that make the element not visible. see: http://php.net/manual/en/class.domdocument.php
The second option is a short answer, it would take multiples step to get it done and i'm not sure how since i never made it myself but studying the manual make me say it is possible.
If the target is cross-domain you can accomplish this by scraping the external page into a php holder page using curl, then loading that php holder page as a jQuery Ajax function and the :hidden selector.
holder.php
$ch = curl_init("http://www.foo.com/bar");
$html = curl_exec($ch);
echo $html;
page.php or page.html
$.get('holder.php', function (data) {
hidden_tags = $(data).find('a:hidden');
});

Echo the src value of an iframe on a page

If I have a file called 'index.php' and this file contains a lot of HTML lines...
Also (index.php) have this iframe:
<iframe src="http://test.com" />
How I can use PHP to get the src which is "http:/test.com" ... so it will be like that:
$getiframesrc=THE_CODE_WHICH_I_WANT_SOMEONE_TO_TELL_ME_ABOUT_IT;
And I can easily echo the src of the iFrame by echo $getiframesrc;
For example: If I want to make a browser using PHP, I want the URL Address Box's text to be the value of the iframe src (THIS IS ONLY AN EXAMPLE!!!)
So, please guys tell me what should be :
"THE_CODE_WHICH_I_WANT_SOMEONE_TO_TELL_ME_ABOUT_IT" .
EDIT: $getiframesrc will be in index.php too!
And thanks :-)
you can use ajax and jquery to get the src value then send it to the php file
Jquery
$(document).ready(function(){
var vidsrc = $("#iframeid").attr("src");
$.post( "index.php", { videosource: vidsrc });
});
index.php
if (isset($_POST["videosource"]))
{
$videosource = $_POST["videosource"];
// code to be excuted
}
Here's a working example -- and make sure you close your <iframe> tag.
$('button').click(function(){
var src = $('iframe').attr('src');
alert( src );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button>Read iframe src</button>
<iframe src="http://test.com"></iframe>
To re-use the src variable elsewhere on the page, just declare it outside the $('button').click(function() function -- or even outside the $(document).ready() fn.
EDIT:
To get the variable data to PHP... By the time the javascript runs, the DOM has been rendered. PHP has finished execution and will not run again on that page.
So, what to do? How to get the variable into PHP? You have two choices:
(1) Use a form - When submitting a form, the data is sent to the PHP file specified in the action= attribute on the <form> opening tag:
<form action="your_secondary_php_file.php" method="post">
Downside to a form is that user is navigated away from the page, or (at the very least) the page is refreshed.
(2) Use AJAX. AJAX (very simple, not to worry) will send your data to a back-end PHP file, the PHP file can do something with that data, and then it can (optionally) send new data/HTML/text/whatever back to the AJAX code block.
Advantage of using AJAX - will not refresh or move away from the current page. All user-entered data remains as is, and you can pro-grammatically receive data back from the PHP side and dynamically update the page with the new data. Magic by another name.
This answer contains some simple examples of AJAX.
Firstly, thank you very much #gibberish ( gibberish ) for your answer, it's the best answer for me and I'm using it now :-) .
I figured out how to do that with PHP (thanks for #gibberish to help, because his example helped me.) - But sorry :/ I can't say how I did that because it's very hard coded (everything is manual in that) ... so I will simplify it and post the answer :-)
Next to the #gibberish answer, we can use PHP GET variable and set the iFrame src with it.
Example:
PHP Part in index.php :
<?php
$iframesrc=$_GET['iframesrc'];
?>
HTML Part in index.php :
<iframe src=<?php echo $iframesrc; ?>></iframe>
And then we can access http://Mysite.tld/index.php?iframesrc=http://test.com ;)
So now, I can code well - like that:
if($iframesrc !=="http://test.com")
{
//code
}

obtain url paramethers from a page called with JQuery's load() function

I have seen this question before but I haven't found a working solution.
The question is quite easy.
If I call a page like this.
div.load('page?foo=bar');
I want to be able to retrieve foo in some way an use it in a javascript called by page. But I only manage to obtain the paramethers of the parents url.
I know I can declare variables in the parents javascript code but that is not my preffered way.
So I really hope someone has a solution to this problem.
♥ you guys
You could use something like this to parse the URI:
http://blog.stevenlevithan.com/archives/parseuri
Then you can access the parameters easily from the parent page:
// Set the link that we want to load/examine
var link = 'page?foo=bar';
// Load the link content (as per your code)
div.load(link);
// Grab whatever variables we want from the link
var uri = parseUri(link);
var foo = uri.queryKey.hasOwnProperty('foo') ? uri.queryKey.foo : false;
alert(foo);
EDIT:
As bfavaretto already commented, the content loaded in via AJAX is just a string. It's not a page that will be aware of its URI.
However, if you really want the loaded content to be able to access its URI, just make it available in the content itself. For example:
$('#my_div').load('page?foo=bar)
And in the content of "page?foo=bar":
<div class="container" data-page-uri="{{ insert uri here with php, ruby, whatever }}">
<!-- my page content -->
</div>
Now in your loaded content, you can determine the URI by finding the relevant div with the "data-page-uri" data attribute. Once you have the link, you said that you know how to grab the parameters from it...
Hope that helps.
I think you have two solutions. One, if page has a hidden div, with the data needed, the second one, probably the ajax response object has the caller url. You should study the response xhr object.

load a document string into an iframe

I have a string (fetched via ajax), which is an entire html document (doctype to < /html>). Does anyone know of a way to load it into an iframe?
I cannot simply specify the url that returned the document in the src of the iframe, since the response may have come from a post, and repeating it may have ill effects. Also, I can't submit it to the iframe the first time, since I can't predict absolutely that the result will be a document and not some json. Basically, I can't recall the url, I must be able to use the version I have (a string).
jQuery is fair game, since that's what I'm using.
You can do this using a data URI. A data URI is a way to load inline data as if you're loading external data. They look like this: data:<mimetype>,<data>. For HTML, the mimetype is text/html, and in your case, the data is something like this: <!DOCTYPE><html><head><title>Iframe</title></head><body>Hi!</body></html>. If we put this in a data URI, we get something like the following:
data:text/html,<!DOCTYPE><html><head><title>Iframe</title></head><body>Hi!</body></html>
When we set this as the src of the iframe, it looks like this.
var string = '<!DOCTYPE><html><head><title>Iframe</title></head><body>Hi!</body></html>',
iframe = $('#iframe')
iframe.attr('src', 'data:text/html,' + string)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="iframe" src=""></iframe>
#LarsW one doesn't need to use jQuery. For example, if the html string is a file that resides in the filesystem but it is not accessible from www (or you have to construct it in PHP), you can use this PHP code
$htmlText = "data:text/html;base64,".base64_encode(file_get_contents("someFolder/file.html"));
echo "<iframe src='$htmlText'></iframe>";
I've had problems without base64 extension because quotes in file.html can break the HTML code.

Categories

Resources