Note that there are some explanatory texts on larger screens.

plurals
  1. POText selection and display in context menu chrome extension
    primarykey
    data
    text
    <p>I have been working on a small Chrome extension with a problem I cant seem to get my head around and would appreciate someone to look at it with a fresh perspective.</p> <p>The goal is to create a chrome extension which enables you to select text on any given website and displays an option to send the selection to another website using a contextmenu item.</p> <p>My <strong>manifest.json</strong> looks like this:</p> <pre><code>{ "name": "Context Menu Search", "description": "Opens the selected text as keyword in a new window", "version": "0.1", "permissions": ["contextMenus"], "background_page": "background.html" } </code></pre> <p>Then <strong>background.html</strong>:</p> <pre><code>&lt;script src="rightclick.js"&gt;&lt;/script&gt; </code></pre> <p>And <strong>rightclick.js</strong>:</p> <pre><code>var selection_callbacks = []; function getSelection(callback) { selection_callbacks.push(callback); chrome.tabs.executeScript(null, { file:"selection.js" }); }; chrome.extension.onRequest.addListener(function (request) { var callback = selection_callbacks.shift(); callback(request); }); function sendSearch(selectedText) { var serviceCall = 'http://www.google.com/search?q=' + selectedText; chrome.tabs.create({url: serviceCall}); } var tx = getSelection(); var title = "Test '" + tx + "' menu item"; var id = chrome.contextMenus.create({"title": title, "contexts":[selection], "onclick": sendSearch(tx)}); console.log("selection item:" + id); </code></pre> <p>Plus <strong>selection.js</strong>:</p> <pre><code>chrome.extension.sendResponse(window.getSelection().toString()); </code></pre> <p>So far the context menu creation works fine, but the selected text is not displayed at all. If anyone has any suggestions on how to solve this problem as well as simplify the script, I would appreciate your input.</p> <p>Thank you very much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload