How extension get the text selected in chrome pdf viewer? - google-chrome

I wrote a chrome extension - english dictionary. You select word, the definition appears.
It works well, but I counter a problem. It seems there is no api of chrome pdf viewer supplied by google.
How can I get the word when i select a word in pdf using chrome pdf viewer?
I will be appreciate if you could help me.

You can get selected text using the context menu. In your background script, adding these lines will allow the user to right-click and do something with the selectionText.
chrome.contextMenus.create({id:"lookup",title:"Lookup %s",contexts:["selection"]});
chrome.contextMenus.onClicked.addListener(function(sel){
console.log(sel.selectionText);
});
Grabbing this text works fine with PDFs, whether part of the extension, or not.
However, you cannot inject a script into a page starting with "chrome-extension://". If this is how your extension works, that will not be (directly) possible. But getting the selected text, and doing something with it still very doable.
As an alternative to requiring script injection, see the notification api, which allows a small message to popup, which could contain the definition of the word.

Related

Replace HTML text displaying on webpage using Chrome without installing a chrome extension

I am trying to find a way to replace certain text on the webpage without the use of chrome extensions.
For example, I want the text 'dogs' to replace with 'doggos' each time the web page detects the word dogs. Is it possible to do this without the use of Chrome Extensions? How can I write such a script and can this run in the backgroun?
Thanks all!
Use Chrome Dev Tools
If you want to use chrome, but not a chrome extension, you want to use the console in the Chrome Dev Tools. Running a script for a website on the Chrome Console is a solution to your question, and is shown below, but it will disappear when you refresh the page.
Replacing String Literals in the Body
To replace all instances of dogs with doggos in the HTML body, you would walk the document tree as documented by this SO question.
Applied to your question, the result is below:
Further Reading
You may also want to check out javascript with Regular Expressions, which are more powerful than a simple string replace. If you want this running in the background, you should look at Mutation Observers to check whether the DOM has changed.

Accessing visible PDF with Chrome Extension Content Script

I'm trying to build a Chrome extension to access the binary of a PDF in the case of a user viewing that PDF in a tab in Chrome. I thought that I would use a Content Script to get around file permission issues, and now I'm trying to figure out how to access the binary of the PDF via PDFium through the DOM. Is this possible? Has anyone found a good solution for this? I don't need to do any rendering, I just need the binary representation of the rendered file.

How to edit raw HTML with Greasemonkey/Tampermonkey before it is parsed by browser

Does greasemonkey or tampermonkey have functionality that lets me edit the raw HTML response before it gets passed to the browser?
I'm trying to do this in an attempt to modify an inline script before it executes. This solution must work on both Firefox and Chrome so something like beforescriptexecute won't work because chrome doesn't support it yet.
If not, is there an extension that does provide this functionality that is available to both browsers?
Quick answer: No.
Longer answer: Tampermonkey and Greasemonkey operate on editing the page has loaded, not at any time prior to that. So, preventing a script from executing isn't going to happen. Neither of these tools can change the code before it's run, it only alters once it has run and can inject certain scripts into the page after it's loaded that affect the data displayed or the appearance. The best you could do is update or replace what's displayed based on the inline script, but it's still going to get sent initially.

add a TAB specific notification (jquery ui dialog) in chrome extensions?

I a writing an extension with a background.js file that compares the url (every time it changes) with an internal list. If it finds a match, I want to have a popup that shows the user various options.
Currently, as part of my quest to understand extensions, when you visit one of the urls in the list it pops up a Desktop Notification. That is definitely NOT what I want since it is hard to customize and is not specific to the tab.
My ultimate goal is to have something similar to the jquery ui modal dialog. I've tried searching, but the similarity to Desktop Notifications makes it difficult. Is it even possible?
after reading more on how to inject JS into a page, I stumbled across a simple way that does not use the content_script paremeter (I don't want to add code to every page and the list of "matching" urls is both long and dynamic). This code can be run from the background.js file (as long as I insert "http://*/" and "https://*/" in permissions inside manifest.json):
chrome.tabs.executeScript(tabId, {code:"alert('this url matches your list');", runAt:"document_start"});
I wrapped that in a function that only gets called AFTER finding out if the url matches one on my (long) list of urls
technically, this answers my question...it would be possible to change that js "code" that I am executing so that it looked a lot nicer and functioned the way I wanted it to...
BUT, that seems like a lot of work, and adding jquery doesn't seem to be an options...anyone have a different method that is simpler?

How to access input fields in a page from a Chrome extension?

I am trying to make a Chrome extension for personal use that makes all the password fields type="text", so I can see the clear passwords. (I know that I can probably find something like this in the Chrome store, but it's something I want to do myself).
I am however having a hard time accessing the actual page content (the page I'm viewing in the tab). If I do
document.getElementById('text')
this selects the element from background.html not the page I'm viewing. How can I access the actual page?
Also, is there a way I can include an external javascript file so I can use functions from there?
You would need to use content script - javascript file injected into an actual page with access to DOM and events.