Created
January 25, 2014 06:49
-
-
Save umidjons/8612712 to your computer and use it in GitHub Desktop.
JS/JQuery: add copyright text on copy event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery( document ).ready( function( $ ) | |
{ | |
function addLink() | |
{ | |
var sel = window.getSelection(); | |
var pagelink = "<br /><br /> Source: <a href='" + document.location.href + "'>" + document.location.href + "</a><br />© text is here"; | |
var div = $( '<div>', {style: {position: 'absolute', left: '-99999px'}, html: sel + pagelink} ); | |
$( 'body' ).append( div ); | |
sel.selectAllChildren( div[0] ); | |
div.remove(); | |
} | |
document.oncopy = addLink; | |
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addLink() { | |
var body_element = document.getElementsByTagName('body')[0]; | |
var selection; | |
selection = window.getSelection(); | |
var pagelink = "<br /><br /> Read more at: <a href='"+document.location.href+"'>"+document.location.href+"</a><br />Copyright © text is here"; // change this if you want | |
var copytext = selection + pagelink; | |
var newdiv = document.createElement('div'); | |
newdiv.style.position='absolute'; | |
newdiv.style.left='-99999px'; | |
body_element.appendChild(newdiv); | |
newdiv.innerHTML = copytext; | |
selection.selectAllChildren(newdiv); | |
window.setTimeout(function() { | |
body_element.removeChild(newdiv); | |
},0); | |
} | |
document.oncopy = addLink; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jquery example give an error...
BrowserLocker: checkFramesTimer() content_script.js:92
[ERROR] checkForNewIframe: SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://www.exampledomain.com" from accessing a cross-origin frame.