简体   繁体   中英

Getting started with developing for Chrome

I have been asked by my friend to make an application for Chrome and it requires me to have context-sensitive menus as below:
在此输入图像描述

I have never really made anything for Chrome before and I have a few questions regarding it:

  1. I will have to develop a plug-in , right ?
  2. If so, is there a specific set of rules I have to follow ?

I know I can use GWT to compile Java to JavaScript
3. This context sensitive menu is the same as JPopupMenu ?

The application I want to develop is simple:
Copy some text,
right-click, click on the context sensitive menu
apply simple Caesar's cipher to the text
open a new JFrame with JtextArea in it to display the encrypted text.

  1. What you're creating is called an " extension ", not a "plug-in". A browser extension is written using HTML, CSS and Javascript, and got access to APIs for direct interaction with the browser.
    Plug-ins , on the other hand, are compiled binaries such as Flash and Java.

  2. Drop the idea of using GWT for Chrome extensions. It makes development of the extension harder, not easier ( open issue ).
    Especially because you'll find plenty of vanilla JavaScript examples and tutorials in the documentation and Stack Overflow.

You just have to know the relevant APIs:

Copy some text, right-click, click on the context sensitive menu

Use chrome.contextMenus . There's no need to copy, the selected text is available in the callback ( examples ).

apply simple Caesar's cipher to the text

Create a JavaScript function to achieve this.

open a new JFrame with JtextArea in it to display the encrypted text.

Create a new window using chrome.windows.create . You could include an extra HTML page in your extension, and use the message passing APIs to populate the text field, but since you appear to be a complete newbie, I show a simple copy-paste method to create and populate this window:

function displayText(title, text) {
    var escapeHTML = function(s) { return (s+'').replace(/</g, '&lt;'); };
    var style = '*{width:100%;height:100%;box-sizing:border-box}';
    style += 'html,body{margin:0;padding:0;}';
    style += 'textarea{display:block;}';
    var html = '<!DOCTYPE html>';
    html += '<html><head><title>';
    html += escapeHTML(title);
    html += '</title>';
    html += '<style>' + style + '</style>';
    html += '</head><body><textarea>';
    html += escapeHTML(text);
    html += '</body></html>'

    var url = 'data:text/html,' + encodeURIComponent(html);
    chrome.windows.create({
        url: url,
        focused: true
    });
}

Don't forget to read Getting started to learn more about the extension's infrastructure.

Check out Google Chrome Extensions Chrome Extensions

The Getting Started will help you Getting Started

You will find a section on how to use Context Menus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM