How to run user scripts on Firefox's built-in PDF Viewer? - javascript

I'm running a simple Greasemonkey/Tampermonkey user scrip in Firefox which gets selection of a text (let's say, just a single word) and opens a translator for it.
// ==UserScript==
// #name translator
// #version 1.0
// ==/UserScript==
document.addEventListener('dblclick', handleDblClick, true);
function handleDblClick(e) {
var txt = window.getSelection().toString();
window.open("https://translate.google.ru/?hl=ru&text=" + txt);
}
This works fine with ordinary web-pages, but not with built-in PDF Viewer. Is there any chance to make this (or any other) script running while viewing PDF-files in Firefox?
Here is a small sample of PDF-file to try with: http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf

I have a similiar problem and came to the conclusion that your desired behaviour is unfortunately not possible after Firefox 60. However, bookmarklets work in the built-in PDF viewer. One possible fix would be to rewrite your userscript to work as a bookmarklet.
Here is a bug report indicating that the current behaviour is intentional: https://bugzilla.mozilla.org/show_bug.cgi?id=1454760
Good luck!

Related

Super simple script runs on some pages, not others. Why?

I have a super simple script (running in a Userscript manager for Safari) that works on some pages but not all.
Here is the script:
// ==UserScript==
// #name Close tab on double click
// #include https://*
// ==/UserScript==
document.addEventListener("dblclick",function (event) {
window.close();
});
For some reason, it works on Stack Overflow, Reddit post pages, etc., but does NOT work on Reddit homepage, Google (homepage or search results), etc.
Why?
Thanks for any insight. I'm still a newbie about javascript.
Edit: Working now, using the GM.closeTab permission from my Userscript manager, as documented here: https://github.com/quoid/userscripts#api
Try using
// #grant window.close
in order to use window.close per the documentation.
In a chrome browser, when I add the JS function in the console and then double click on the page I get this notification (I tried the stack overflow site):
Console Image
My guess is your userscript manager may add the script you write differently depending on the website, and the websites that don't work are because of this warning. Try opening your console after you add the script and see if you get this warning when double clicking does not work.

Simulating drop event works in Chrome extension but not Firefox add-on?

I'm trying to simulate a drop event (with the Drag and Drop API) in a browser extension's content script. Essentially, I want to simulate a user dropping an image programmatically. I have this so far:
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file); // File object representing image being dropped
const event = new DragEvent('drop', { dataTransfer });
document.body.dispatchEvent(event);
This works great on both Chrome and Firefox when all the simulation happens natively in the browser. This JSFiddle works great in both browsers–if you upload a file, a drop is simulated and the image shows up in the preview.
The problem is when I try to accomplish the exact same thing with a browser extension, I run into problems in Firefox (but not Chrome). I've put together a demo extension that accomplishes the same thing as the JSFiddle but with a content script. On Chrome, the extension injects a file input and simulates the drop perfectly (the image shows up in #preview):
You can see (in the free-hand circle) that dataTransfer.files has a length of 1–the dropped image file is in the list. But when I use the exact same extension on Firefox:
The image was uploaded (see the input) but it was not dropped and shown in the preview. From the console, you can see that dataTransfer.files is empty in Firefox, even though dataTransfer.items has the file in it!
Why is there a discrepancy here? I checked with Mozilla's compatibility checker which said my extension was cross-platform ready. By then HTML5 spec files should be in sync with items, and no conditions AFAICT warrant an empty list on Firefox. Could this be a bug?
Thanks to wOxxOm, the problem was due to Firefox's Xray vision policy that separates global objects of content scripts and page scripts (including File). By unwrapping the page's DataTransfer object instead of the content script's, I was able to simulate the drop:
const dataTransfer = new window.wrappedJSObject.DataTransfer();

Which JavaScript functions are called when page loads?

Is there any way to find out which JavaScript functions are called when a page is loaded?
One way is to use alert but if a file is too big ( in my case 5000+ lines ), it would be too difficult to use alert.
I want to find out is there anything in FireBug or Developer Tools that shows the order in which functions are called
Thanks
By the way, thanks to the person who gave negative comment
The Simple Solutions(tedious for you if you have 5000+ line codes) To Your Queries is
Debbuging
There are many way you can debug Your Javascript code
A)The Chrome DevTools include a number of useful tools to help make debugging JavaScript less painful.|
The Sources panel lets you debug your JavaScript code. It provides a graphical interface to the V8 debugger. Follow the steps below to explore the Sources panel:
Open a site on chrome
Open the DevTools window.
If it is not already selected, select Sources.
B)You can use FireBug Chrome tool But i Beleive Chroe Debugger is handy
but Choose Opera For Optimum result
path
Internet Explorer 8 (Developer Tools - F12). Anything else is second rate in Internet Explorer land
Firefox and Firebug. Hit F12 to display.
Safari (Show Menu Bar, Preferences -> Advanced -> Show Develop menu bar)
Google Chrome JavaScript Console (F12 or (Ctrl + Shift + J)).
Mostly the same browser as Safari, but Safari is better IMHO.
Opera (Tools -> Advanced -> Developer Tools)
use window.onload is perfect to your request
window.onload = function(){
alert("the page is loaded!!!");
};
it was tested onload=function vs window.onload=function
window.onload // works in all tested browsers
onload // works in all tested browsers, faster than window.onload
so it can't fail... use it, for me is the best way
Live CODE

How can I disable print-screen functionality for a webpage in all browsers?

Using the following we can disable print-screens or screenshots in Internet Explorer:
<body onload=setInterval("window.clipboardData.setData('text','')",2)
oncontextmenu="return false" onselectstart="return false">
But these don't work in Mozilla, Chrome and other browsers.
Is there a better way to disable print-screens/screenshots?
What makes you think it's your decision if people should be able to take screenshots or not?
Luckily no browser but IE allows you to access the clipboard via JavaScript so you are out of luck :)
By the way, if I visited your site and it messed up my clipboard (it overwrites anything in there, even if it's unrelated to your site) - I might have stored something in it that I've just cut from some file and I was going to paste in a different file and thanks to your site it would now be lost.
So, the conclusion is: Stop doing crap like that.
It's an O.S. function, as well as a page function, and a print function so there are a few things you need to do - The steps below are specific for windows, however can be implemented in any O.S. with the same concept -
Disable the print screen at an O.S. Level
Here are the steps of disable Print Screen key:
1.Copy the following registry to notepad and saved as a .reg file.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,04,00,00,00,00,00,2a,e0,00,00,37,e0,\
00,00,54,00,00,00,00,00
2.Apply the registry
3.Sign out and sign in again.
Then you need to block the browsers capability of capturing screens in the cases where chrome or edge or firefox have extensions that enhance print screens -
And for extra measure, disable right click (I put it on document but you can put it per DOM
document.addEventListener('contextmenu',
event => event.preventDefault());
window.addEventListener("keyup",kPress,false);
function kPress(e)
{
var c=e.keyCode||e.charCode;
if (c==44) event.preventDefault();
}
Then as an extra, to disable printing and items, you need to mark the print media as display none
#media print {
.noprint {
visibility: hidden;
}
}
And if you want to be POPIA/GDPR compliant, you have to disable things like pdf download, object references and things so as a bonus item, Use PDF.js to render the pdf as html with full control over the rendering of the PDF, download and printing using the above
This reference allows password entries and successfully gave us full control over all features for capturing or saving information from protected sites
https://usefulangle.com/post/22/pdfjs-tutorial-2-viewing-a-password-protected-pdf
window.addEventListener("keyup",kPress,false);
function kPress(e)
{
var c=e.keyCode||e.charCode;
if (c==44) alert("print screen");
}

iframe undefined in Greasemonkey script

I have created a Greasemonkey script that runs fine in the firebug editor, with the Greasemonkey specifics removed, but not when I try to package it as a userscript. The Firefox error console is reporting that an iframe I am trying to use is undefined.
I've cut the userscript down to a minimum case where it should be printing the iframe html into the firebug console, and does when run in the firebug editor, but is not working as a userscript:
// ==UserScript==
// #name Movies
// #include http://*.princecharlescinema.com/*
// #include http://princecharlescinema.com/*
// ==/UserScript==
// stop script loading multiple times
if (top !=self) return;
var iframeHTML = window.frames['iframe2'].document.documentElement.innerHTML;
unsafeWindow.console.log(iframeHTML);
An example page the script is intended for
If it's of any use the gist of the full script is I collect all the td tags from this iframe, get some information from them, and then insert some new html into some of the same td tags.
Any help would be appreciated.
Perhaps you need to wait for DOMFrameContentLoaded

Categories