Pass selected text to default search engine - javascript

I am working on a Chrome/FF Quantum extension, and I've run into a problem. This extension creates a floating toolbar via content script, when the user selects some text on the webpage.
The toolbar has an "Open" button. What I want to do is to open a new tab, and pass the selected text to it. If the selected text is a valid URL, it should be opened. If it's only arbitrary text, it should be passed to the default search engine of the browser. Just like when the user types into the addressbar.
So far I have a content and a background script. The content script reads the selected text and sends it to the background script with chrome.runtime.sendMessage(). In that script I've tried both the chrome.tabs.create() and the window.open() methods, but if the selection is only arbitrary text, the new tab loads with the URL of the extension itself, and attaches the text to the path part.
Is it even possible to do, or I have to use a search engine as a user setting of the extension, and build the query myself?

Related

Chrome extension: Inject a script into a single webpage (provided by the user)

I have a chrome extension with a single text input field and a button that says "Test banner". My goal is to inject a script that will manipulate the DOM of the given webpage and add a big red banner at the top, but I only want to inject it into the single url that I put in the text field.
Basically my options.html has a text input and a button. When the user enters a URL like google.com I want to open the page and inject into only that instance of the page. My issue is that
$('#start-button').button().click(function() {
var url = $('#url').value;
window.open(url)
});
My url successfully opens, but I "wish" I could "open and inject". Since I can't do that I've been relegated to trying to setup a background.js that listens for a window being opened, but that code keeps injecting into every tab with the url that I set (example. If I type google.com, then it will keep injecting into every google.com tab that I open).
So my question is... is there some kind of api available where I can do window.openAndInject(url)?
Note: I am very very new to this. Please excuse any noob mistakes I may have made.

Bookmarklet to go to URL based on selected text

How could I write a bookmarklet for Google Chrome that will take the selected text, append it to a predetermined URL, and then go to the modified URL.
For example, let's say the base URL is http://www.mybaseurl.com/. (This base URL is hardcoded in the bookmarklet code.) Now, suppose that on a random webpage I select the text dog. Then, if I click the bookmarklet while that text is selected, I want the bookmarklet to cause the browser to visit the following URL: http://www.mybaseurl.com/dog.
How can this be done?
You can get the currently selected text with window.getSelection(). So this bookmarklet can redirect based on the selected text:
javascript:window.location.href="http://www.mybaseurl.com/"+window.getSelection()
This method will open the url in a new window or tab (depending on browser settings), instead of opening the url in the current tab. So, you won't lose your place. It uses window.open instead of location=
javascript:(function(){s=document.selection?document.selection.createRange().text:window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection():'';if(s==''){s=prompt('You%20did%20not%20select%20any%20text%20to%20search%20for.%20Enter%20the%20text%20to%20search%20for%20:','');}if(s){window.open('https://mxtoolbox.com/SuperTool.aspx?action=ptr%3a'+s, '_blank')};})()

Google Chrome Extension, new tab focus

I've been working on an extension for Google Chrome, this is my first attempt.
So far I've been able to run my extension in a new tab, but I wanted to know if there was any possibility to remove the focus from the omnibar/addressbar to some content in the tabbed window.
For Example, I'm implementing an input field or text area, and when a new tab opens, I want the blinking cursor/caret to originate in the said text area, and not the google omnibar.
Any possibility using jquery or any google chrome extension api?
I had the same problem. My solution was to use a "dummy html" file as the new page, and everything this page did was running the following JavaScript code (in an external JS file linked to from the dummy html file):
window.open("the-real-new-tab-page.html")
window.close()

Load 2nd web page in background to get content from this web page

I couldn't find anything about this topic, maybe because I'm not a good english speaker and can't find the right words to search for in google therefore.
I'm currently working on an Chrome browser extension which enables me to search for user accounts on a specified web page by using the context menu. Example: If I select the text name123 on any webpage and click the context menu entry, a new tab http://www.webpage.de/user/name123/ is opened.
Now I want to extend the extensions capabilities: I want to search for email addresses, too. For that I can use http://www.webpage.de/search/name123#mail.com/ which brings up a list of all user with the email address name123#mail.com. From there, I could extract the link to the user account as follows:
document.getElementsByClassName("xyz")[0].href
My question: Can I skip the loading of http://www.webpage.de/search/name123#mail.com/ and the "extraction" of the url to the user account? Respectively, can I hide the procedure of opening this additional web page? Does JavaScript support any kind of "preloading" of a webpages content, without being displayed in the browser?
If you can parse the required link from raw HTML, you can fetch the page via an XMLHttpRequest in the background page, examine the result and then open the real profile page.
If the page is dynamic and you need its scripts to run before you can extract the link, you can load it in an iframe in the background page and examine it.
You can do it.
Add permessions to your manifest.json file: 'http://www.webpage.de/*'
Create neccesary ajax requests from background script.

Chrome extension that extracts automatically some data and displays them to the user

What i want to do is to create an extension for chrome that
will be active only when the user browse specific pages.
the main task will be when the coursor is hovering over a link, a number will appear next to the cursor.
that number will be extracted from the page's source code of the link
any suggestions on how i could do that? thanks in advance!
You should do a content script, triggered by the specific pages.
http://code.google.com/chrome/extensions/content_scripts.html
Chrome injects automatically your content script in pages matching the pattern you indicate in manifest.json:
http://code.google.com/chrome/extensions/manifest.html
http://code.google.com/chrome/extensions/match_patterns.html
The content script lets you interact with the page's DOM, thus you can find the anchors ( tags). You may hang a listener from mouseover event, or you can just write the anchors title attribute (the text that popup when hoovering).
If your specific pages list isn't static (you want to modify the list without modifying the extension), then you may want to learn about programatic injection.
http://code.google.com/chrome/extensions/content_scripts.html#pi
If you choose to hang a listener, use addListener, don't use the mouseover attribute, because your listener function isn't in the same world, it's on an isolated world.

Categories