Chrome插件 - 上下文菜单插入文本 [英] Chrome plugin - Contextmenu insert text

查看:99
本文介绍了Chrome插件 - 上下文菜单插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在右键单击输入字段(可编辑)时插入预定义的字符串,通常我会使用文档get元素。但是,由于我无法确定用户点击哪个元素,因此在任何网站上,我该如何去做这件事?



到目前为止,我有我的清单文件

 permissions: [
contextMenus,
background,
https://ajax.googleapis.com/
],

background :{
scripts:[scrippy.js]
}
}

(我还有其他所需的信息。)



和我的脚本文件

  //创建上下文菜单并仅设置输入
var type = [editable];
var scrippyMenu = chrome.contextMenus.create({title:Scrippy,
contexts:type});


//菜单上下文
var menuContext = chrome.contextMenus.create({title:blabla,
parentId:scrippyMenu,contexts :type});
$ b $ var menuChild1 = chrome.contextMenus.create(
{title:time,parentId:menuContext,contexts:type,onclick:
genericOnClick});

函数genericOnClick(info,tab){

document.getElementById(Theunknownelement)。textContent ='This is added text';


$ / code $ / pre

解决方案

一个基本的误解:你的后台脚本不能修改网页,因为文档引用了后台页面本身。 b

查看架构概览。完成之后,您应该知道您需要内容脚本



一旦我们确定了,您的内容脚本就可以独立跟踪发生点击的位置,并且包含对元素的引用。然后,当你的背景请求修改时,你知道最后一个这样的元素。



有一个非常好的问题>非常详细地讨论您的问题。


I'm trying to figure out how to insert a predefined string on right clicking a input field (editable), usually i would use the document get element by id. However, since i have no way to determine what element a user clicks, on any website how do i go about doing this?

so far i have my manifest file

"permissions": [
    "contextMenus",
    "background",
    "https://ajax.googleapis.com/"
],

"background": {
    "scripts": ["scrippy.js"] 
}
}

(i have the rest of the required info as well.)

And my script file

// Create context menu and set to only on inputs
var type = ["editable"];
var scrippyMenu = chrome.contextMenus.create({"title": "Scrippy", 
"contexts": type});


// menu context
var menuContext = chrome.contextMenus.create({"title": "blabla", 
"parentId": scrippyMenu, "contexts": type});

var menuChild1 = chrome.contextMenus.create(
{"title": "time", "parentId": menuContext, "contexts": type, "onclick": 
genericOnClick});

function genericOnClick(info, tab) {

document.getElementById("Theunknownelement").textContent = 'This is added text';

}

解决方案

You have one fundamental misunderstanding: your background script can't modify the webpage, because document refers to the background page itself.

Take a look at the Architecture overview. After you do, you should know that you need a Content Script.

Once we've established that, your content script can independently track where the click happened - and that would contain a reference to the element. Then, when your background requests a modification, you know the last such element.

There is a very good question discussing your problem in great detail too.

这篇关于Chrome插件 - 上下文菜单插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆