Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unblock more events #50

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ paste into a text box.

## Solution

This is a dead simple Google Chrome extension that removes copy and paste
blocking.
This is a dead simple Google Chrome extension that removes copy, cut and paste
blocking. It also removes blocking of related features such as
Ctrl key shortcuts, right-clicking, selecting areas to copy,
and the browser context menu.

## Configuration

There are some sites that do helpful things with copy and paste events, and for
those sites you want their paste event handlers to still work. In the options
those sites you want their event handlers to still work. In the options
for this extension, you can add an exclusion pattern that matches the site's
URL, which will prevent this extension from running on that site, and thereby
allowing the paste event to occur.
30 changes: 27 additions & 3 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
const allowCopyAndPaste = function(e){
const allowDefaultAction = function(e){
// prevent custom event handlers on the page swallowing the event
e.stopImmediatePropagation();
return true;
};

const allowDefaultCtrlAction = function(e){
if (e.type !== "keydown") return false;

const CTRL_KEY_CODE = 17;
if (parseInt(e.keyCode) !== CTRL_KEY_CODE) return false;

return allowDefaultAction(e);
};

const allowDefaultRightClickAction = function(e){
if (e.type !== "mousedown") return false;

const RIGHT_CLICK = 2;
if (e.button !== RIGHT_CLICK) return false;

return allowDefaultAction(e);
};

chrome.storage.sync.get(window.defaultValues, function({exclude, include}) {
const excludes = new RegExp(exclude.split('\n').join('|'));
const includes = new RegExp(include.split('\n').join('|'));
const location = window.location.href;

if (includes.test(location) && !excludes.test(location)) {
document.addEventListener('copy', allowCopyAndPaste, true);
document.addEventListener('paste', allowCopyAndPaste, true);
document.addEventListener('cut', allowDefaultAction, true);
document.addEventListener('copy', allowDefaultAction, true);
document.addEventListener('paste', allowDefaultAction, true);
document.addEventListener('keydown', allowDefaultCtrlAction, true);
document.addEventListener('mousedown', allowDefaultRightClickAction, true);
document.addEventListener('contextmenu', allowDefaultAction, true);
document.addEventListener('selectstart', allowDefaultAction, true);
}
});
10 changes: 6 additions & 4 deletions test/iframe.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script src="script.js"></script>
<p>(This is a form inside of an iframe.)</p>
<form>
<label for="unpastable">Go ahead, try to copy or paste</label>
<input id="attribute" type="text" oncopy="return false;" onpaste="return false;" />
<p onselectstart="return false;" >(This is a form inside of an iframe.)</p>
<form onselectstart="return false;" >
<label>Go ahead, try to copy or paste:</label>
<input id="attribute" type="text" oncopy="return false;" onpaste="return false;" oncut="return false;"
onkeydown="return alertOnCtrl(event);"
onmousedown="return alertOnRightClick(event);" oncontextmenu="return false;"/>
<input id="property" type="text" />
<input id="listener" type="text" />
</form>
8 changes: 5 additions & 3 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
<title>I'm fucking with paste</title>
<script src="script.js"></script>
</head>
<body>
<body onselectstart="return false;">
<p>The input boxes below doesn't let you copy or paste stuff in it; every
time a page like this is created, a kitten is killed.</p>
<p>(This is a form in the root document.)</p>
<form>
<p>Go ahead, try to paste:</p>
<input id="attribute" type="text" oncopy="return false;" onpaste="return false;" />
<p>Go ahead, try to cut, copy or paste:</p>
<input id="attribute" type="text" oncopy="return false;" onpaste="return false;" oncut="return false;"
onkeydown="return alertOnCtrl(event);"
onmousedown="return alertOnRightClick(event);" oncontextmenu="return false;"/>
<input id="property" type="text" />
<input id="listener" type="text" />
</form>
Expand Down
44 changes: 38 additions & 6 deletions test/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
function alertOnCtrl(e) {
const CTRL_KEY_CODE = 17;

if (parseInt(e.keyCode) === CTRL_KEY_CODE) {
// popping up an alert box is enough to stop default event handling in chrome!
alert("ctrl shortcuts are blocked");
}

return true;
}

function alertOnRightClick(e) {
const RIGHT_CLICK = 2;

if (e.button === RIGHT_CLICK) {
alert("right click menu is blocked");
}

return true;
}


window.onload = function(){
var preventCopyAndPaste = function(e){ e.preventDefault(); }
document.getElementById('property').onpaste = preventCopyAndPaste;
document.getElementById('property').oncopy = preventCopyAndPaste;
document.getElementById('listener').addEventListener('copy', preventCopyAndPaste, false);
document.getElementById('listener').addEventListener('paste', preventCopyAndPaste, false);
};
const preventEvent = function(e){ e.preventDefault(); };

document.getElementById('property').onpaste = preventEvent;
document.getElementById('property').oncopy = preventEvent;
document.getElementById('property').oncut = preventEvent;
document.getElementById('property').onkeydown = alertOnCtrl;
document.getElementById('property').onmousedown = alertOnRightClick;
document.getElementById('property').oncontextmenu = preventEvent;

document.getElementById('listener').addEventListener('copy', preventEvent, false);
document.getElementById('listener').addEventListener('paste', preventEvent, false);
document.getElementById('listener').addEventListener('cut', preventEvent, false);
document.getElementById('listener').addEventListener('keydown', alertOnCtrl, false);
document.getElementById('listener').addEventListener('mousedown', alertOnRightClick, false);
document.getElementById('listener').addEventListener('contextmenu', preventEvent, false);
};