Copy/Paste functionality on answers.com - php

While working on something non-development related, I noticed something interested while on answers.com. I copied and pasted some text from an answer, and noticed that along with my answer I got some text that I didn't copy. After looking into it some more, it seems that if you copy 8 or more words from anywhere on the site, it adds some text when you paste it. For instance, if I go to a page on the site and copy 8 words or more, and then paste that somewhere, you will get something like:
Insanity is doing the same thing over and over again but expecting
different results.
Read more:
http://wiki.answers.com/Q/Who_first_said_the_definition_of_insanity_is_to_do_the_same_thing_over_and_over_and_expect_different_results#ixzz1xqDl0yUW
My questions is, does anyone know how that is done?! I've looked into the code a bit, but haven't found anything giving me any clues :) Thanks!
P.S. Yes, that page was the one I was viewing when I noticed this :)

function addText() {
var body_element = document.body;
var selection = window.getSelection();
var extraText = "<br /><br />some extra info here";
var copytext = selection + extraText;
var newdiv = document.createElement('div');
newdiv.setAttribute('style','position:absolute;left:-99999px;')
newdiv.innerHTML = copytext;
body_element.appendChild(newdiv);
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
document.oncopy = addText;
Credits: How to add extra info to copied web text

Related

Using PHP to load Javascript

EDIT: My function was broken, with the /n in it. Thanks woofmeow for leading me to question my function!
I am new to programming. I have been trying my best and I have learned a lot the past few months (a bunch here!) but I am stumped.
I have a PHP script that calls from another PHP script and I cannot get the code to actually run. It used to run, then I changed some things and didn't save the changes, I'm not sure what I did. I know, I know, rookie mistake(I learned from it!). The Javascript shows up fine on view page source but doesn't run anymore.
Here is the page source, maybe it is that simple:
<script type="text/javascript">
function delete_user(user_id)
{if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!")) {window.location = "delete_user.php?user_id=" + user_id;}}</script>
Here is the PHP:
The show_users script sends this into the viewer:
$delete_user_script = <<<EOD
function delete_user(user_id)
{
if (confirm("Are you sure you want to delete this user?"
+ "\nThere's really no going back!"))
{
window.location = "delete_user.php?user_id=" + user_id;
}
}
EOD;
The PHP in the HTML was NOT changed from when it was working:
<?php
while ($user = mysql_fetch_array($result))
{
$user_row = sprintf("<li><a href='show_user.php?user_id=%d'>%s %s</a>(<a href='mailto:%s'>%s</a>)<a href='javascript:delete_user(%d);'><img class='delete_user' src='../images/delete.png' width='15' /></a></li>",
$user['user_id'],
$user['first_name'],
$user['last_name'],
$user['email'],
$user['email'],
$user['user_id']);
echo $user_row;
}
?>
And finally, the viewing script that gives us our page source (Note: $embedded_javascript is $delete_user_script):
if (!is_null($embedded_javascript))
{
echo '<script type="text/javascript">' . $embedded_javascript . '</script>';
}
When I mouse over the image to delete the user, it still shows the correct script link ("Javascript:delete_user(%d)", where %d is the user_id) but it's like the function isn't defined anymore, nothing happens. Any ideas are greatly appreciated! Thanks!
Basically your if statement is wrong (even in the $delete_user_script variable). Since it is starting on a new line the interpreter will assume a ; at the end of it and thus your if statement breaks.
Your function has this
if (confirm("Are you sure you want to delete this user?"
+ "\nThere's really no going back!"))
It should be this way
if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))
Sometimes its just a teeny weeny mistake. Hope that helps :)
EDIT 1: this was an error in the origianally posted question in the javascript and php code. Now its been edited to reflect that the if is not broken.
EDIT 2: I have been told this helped to solve the problem so I'll keep this question here. Hope it helps others too.
PS: If someone wants this removed let me know.
Try this instead of what you have now in your script:
$delete_user_script = "".
"function delete_user(user_id)
{
if (confirm(\"Are you sure you want to delete this user?\" + \"\\nThere's really no going back!\"))
{
window.location = \"delete_user.php?user_id=\" + user_id;
}
}";

Leaving website disclaimer through PHP?

Due to legal reasons, I want every external hyper link posted on my forum to first link to a You're leaving this website and are being redirected to a website that is not our property... -disclaimer page. Something like http://www.mydomain.com/?leave=FINALURLHERE would do fine, but how do I set up this system?
I can easily make a script that does it for all URLs, but I only want this to happen to external URLs. Can anybody push me towards the right direction?
Presumably you're using some form of BBCode on your forums. You can just edit that to add your leaving page first.
If you're not, then you'll have to resort to some rather messy JavaScript. Something like:
var links = document.getElementsByTagName('a'), l = links.length, i,
domain = location.protocol+"//"+location.hostname+"/";
for( i=0; i<l; i++) {
if( links[i].href.substr(0,domain.length) != domain) {
links[i].href = "/exit.php?target="+encodeURIComponent(links[i].href);
}
}

using jquery + <audio> w/ php/mysql to loop playback times

I've got a page with an iframe. The iframe is using jquery.scrollTo to detect the audio playback time and keep the contents, lyrics for example, linked to the right point in the audio.
Each line of text is being pulled from a mysql table. What I'm trying to do is to allow the user to click on a line in the <iframe> and have the audio in the parent page play at that point. Here's the code that grabs the times, which exists after the jquery that handles the other audio functions:
<?php
include 'mysqlt.php';
$entries=mysql_query("SELECT * FROM `database`.`table`") or die(mysql_error());
while ($ent = mysql_fetch_object($entries)) {
$segment = $ent->index;
$starttime = $ent->time;
$nextline = $ent->time2;
$length = ($nextline - $starttime); ?>
That same information is also duplicated in the iframe source page along with the individual lines of lyrics. The jquery that uses this info follows:
$("#iframe_id").contents().find("#button_in_iframe<?php echo $segment;?>").bind("click", function(){
audioplayer.currentTime = <?php echo $time;?>;
audioplayer.play();
}
<?php } ?>
This second part loops, echoed once for each each potential line in the iframe that can be clicked. #button_in_iframe<?php echo $segment;?> ends up as something like #button_in_iframe23 for the 23rd line.
I've got jquery loaded up in both the parent and the iframe. As far as I can tell, the syntax is what it should be. If I remove this bit of code, everything works normally. if it's in there, the basic audio functions on the normal controls I have set stop working. So this is not only not working, but it's causing everything else associated with the audio tag's controls to stop working as well. I know that any small error in the audio controls' jquery is enough to make it stop working, but in this case I'm not sure where the problem is.
Thanks in advance for any insights anyone might be able to offer.
update:
I've got it so it no longer crashes the audio tag. Now it's more an issue of the iframe's item not triggering an event. Here's the code now:
$('#iframe_id').contents().find("button.loop2").bind("click", function() {
alert ("test");
});
I'm not getting any console errors except for a Google Maps imbedded in the page in another location. When i click the <button> with class loop2 in the iframe, all of which exist, nothing's happening. That seems to be the root of the problem as it was earlier.
I figured out how to do it. I've set the loop on the parent to read as follows:
play<?php echo $segment; ?> = function(){
sfx.currentTime = sounds.getCueById(id).startTime;
sfx.play();
};
Then in the iframe's page, I've told each button to call parent.play<?php echo $segment;?>();
I'm not sure why finding the tags in the iframe didn't work, but no matter. This get's the job done.
Thanks, shanabus, for all the suggestions.
Would it work to use media cues?
Example:
var sfx = new Audio('sfx.wav');
var sounds = a.addTextTrack('metadata');
// add sounds we care about
sounds.addCue(new TextTrackCue('dog bark', 12.783, 13.612, '', '', '', true));
sounds.addCue(new TextTrackCue('kitten mew', 13.612, 15.091, '', '', '', true));
function playSound(id) {
sfx.currentTime = sounds.getCueById(id).startTime;
sfx.play();
}
You can find a better definition of the spec half way down the page here - http://dev.w3.org/html5/spec/Overview.html
So in your case, maybe something like:
$("#iframe_id").contents().find("#button_in_iframe<?php echo $segment;?>").bind("click", function(){
sounds.addCue(new TextTrackCue('<?php echo $segment; ?>', <?php echo $starttime; ?>, <?php echo ($starttime + $length); ?>, '', '', '', true));
var id = '<?php echo $segment; ?>';
sfx.currentTime = sounds.getCueById(id).startTime;
sfx.play();
}
Or maybe separate your logic into two loops. One for building the cues, then one for building/binding the click events.
Hope this helps!

Auto suggest feature to search records

I don't know what is the exact name of this feature, but I need a feature where the user should be given a text box to search things. When they type in this text box, a matching list of values from database should be shown like a drop-down box.
The user can select one of the value and for will got submit.
I know this is already done on Internet anywhere, but what is this feature called?
Where can I find this feature to download and use for free?
Autocomplete from jQuery UI . SAYT opensource from Google.
http://jqueryui.com/demos/autocomplete/
http://code.google.com/p/search-as-you-type/
I found this code somewhere online, very very useful and very fast (tested for 30000 strings), have been using it ever since,
it uses regex
var rx = new RegExp('"([^"]*' + search + '[^"]*)"', 'gi');
var i = 0;
var num;
results = '';
while (result = rx.exec(string)) {
results += "\n" + result[1];
i += 1;
if (i >= 10) break;
}
I've put up the entire code on JSFiddle
NOTE: My code(The HTML part) is extremely messy, was trying too many things at once, just correct it to suit your requirements.
The easiest way to do this is to use the jQuery UI version here.

Downloading Youtube videos with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am searching for a way to download Youtube videos using PHP. I have searched how to do this for hours but unfortunately all the Google results I find are years old and do not work anymore.
I would appreciate it if someone could explain how to do this, or give a link to an up-to-date article that explains it in detail.
Thanks very much.
The first thing you should do is get a tool like Fiddler and visit a YouTube video page. In Fiddler, you will see all of the files that make up that page, including the FLV itself. Now, you know that the video isn't one of the CSS files, nor is it the image files. You can ignore those. Look for a big file. If you look at the URL, it begins with /videoplayback.
Now, once you've found it, figure out how the browser knew to get that file. Do a search through the sessions (Ctrl+F) and look for "videoplayback". You will see a hit on the first page you went to, like http://www.youtube.com/watch?v=123asdf. If you dig through that file, you'll see a DIV tag with the ID of "watch-player". Within that there is a script tag to setup the flash player, and within that are all of the flash parameters. Within those is the URL to the video.
So now you know how to use your tools to figure out how the browser got to it. How do you duplicate this behavior in PHP?
Do a file_get_contents() on the page that references the video. Ignore everything not in that watch-player div. Parse through the code until you find that variable that contains the URL. From there you will probably have to unescape that URL. Once you have it, you can do a file_get_contents() (or some other download method, depending on what you are trying to do) to get the URL. it is that simple. Your HTML parsing code will be the most complex.
Finally, keep in mind what you are about to do may be illegal. Check the EULA.
Nobody writes manuals/howtos that become outdated every four weeks. The closest you can get is inspecting the actual extraction methods in a contemporary implementation. Quite readable:
http://bitbucket.org/rg3/youtube-dl/raw/2010.08.04/youtube-dl
If you don't want to read through/reimplement it, it's obviously not simple, you could just run it as-is from PHP:
system("youtube-dl '$url'");
last time i was working on fixing one of the brocken chrome extension to download youtube video. I fixed it by altering the script part. (Javascript)
var links = new String();
var downlink = new String();
var has22 = new Boolean();
has22 = false;
var Marked=false;
var FMT_DATA = fmt_url_map;//This is html text that you have to grab. In case of extension it was readily available through:document.getElementsByTagName('script');
var StrSplitter1='%2C', StrSplitter2='%26', StrSplitter3='%3D';
if (FMT_DATA.indexOf(',')>-1) { //Found ,
StrSplitter1=',';
StrSplitter2=(FMT_DATA.indexOf('&')>-1)?'&':'\\u0026';
StrSplitter3='=';
}
var videoURL=new Array();
var FMT_DATA_PACKET=new Array();
var FMT_DATA_PACKET=FMT_DATA.split(StrSplitter1);
for (var i=0;i<FMT_DATA_PACKET.length;i++){
var FMT_DATA_FRAME=FMT_DATA_PACKET[i].split(StrSplitter2);
var FMT_DATA_DUEO=new Array();
for (var j=0;j<FMT_DATA_FRAME.length;j++){
var pair=FMT_DATA_FRAME[j].split(StrSplitter3);
if (pair.length==2) {
FMT_DATA_DUEO[pair[0]]=pair[1];
}
}
var url=(FMT_DATA_DUEO['url'])?FMT_DATA_DUEO['url']:null;
if (url==null) continue;
url=unescape(unescape(url)).replace(/\\\//g,'/').replace(/\\u0026/g,'&');
var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;
var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;
if (itag==null) continue;
var signature=(FMT_DATA_DUEO['sig'])?FMT_DATA_DUEO['sig']:null;
if (signature!=null) {
url=url+"&signature="+signature;
}
if (url.toLowerCase().indexOf('http')==0) { // validate URL
if (itag == '5') {
links += '<span class="yt-uix-button-menu-item" id="v240p">FLV (240p)</span>';
}
if (itag == '18') {
links += '<span class="yt-uix-button-menu-item" id="v360p">MP4 (360p)</span>';
}
if (itag == '35') {
links += '<span class="yt-uix-button-menu-item" id="v480p">FLV (480p)</span>';
}
if (itag == '22') {
links += '<span class="yt-uix-button-menu-item" id="v720p">MP4 HD (720p)</span>';
}
if (itag == '37') {
links += ' <span class="yt-uix-button-menu-item" id="v1080p">MP4 HD (1080p)</span>';
}
if (itag == '38') {
links += '<span class="yt-uix-button-menu-item" id="v4k">MP4 HD (4K)</span>';
}
FavVideo();
videoURL[itag]=url;
console.log(itag);
}
}
You can get separate video link from videoURL[itag] array.
Above logic can be converted to PHP easily
The extension can be downloaded from location http://www.figmentsol.com/chrome/ytdw/
I hope this would help someone. This is working solution (date:06-04-2013)

Categories