javascript - 使用键盘快捷键运行 chrome 扩展
问题描述
我对 js 有非常基本的了解,并且正在开发 Google Chrome 扩展程序。我的目标是创建一个扩展,以便选择(突出显示)一些文本并打开一个弹出窗口,其中的结果来自查询。
这段代码运行良好,但是,我需要打开扩展程序并单击一个按钮来执行此操作。请你帮助我好吗?
这是js代码
/* The function that finds and returns the selected text */
var funcToInject = function() {
var range = window.getSelection().getRangeAt(0);
var selectedText = range.cloneContents().textContent;
return selectedText;
};
/* This line converts the above function to string
* (and makes sure it will be called instantly) */
var jsCodeStr = ';(' + funcToInject + ')();';
window.addEventListener('DOMContentLoaded', function() {
/* Find our input elements from `popup.html` */
var inp = document.querySelector('input[type="text"]#inp');
var btn = document.querySelector('input[type="button"]#btn');
/* Open a new tab with the search results */
btn.addEventListener('click', function() {
var query = encodeURIComponent(inp.value);
chrome.windows.create({ type: 'popup', height: 600, width:600, url: url + query });
});
/* Inject the code into all frames of the active tab */
chrome.tabs.executeScript({
code: jsCodeStr,
allFrames: true
}, function(selectedTextPerFrame) {
if (chrome.runtime.lastError) {
/* Report any error */
alert('ERROR:\n' + chrome.runtime.lastError.message);
} else if ((selectedTextPerFrame.length > 0)
&& (typeof(selectedTextPerFrame[0]) === 'string')) {
/* The results are as expected,
* populate the "search-query" input field */
inp.value = selectedTextPerFrame[0].trim();
}
});
});
谢谢
解决方案
推荐阅读
- reactjs - bundle.js 未呈现 / 404 bundle.js 未找到
- excel - Excel中非常具体的多条件格式
- c# - 托管在 IIS/Asp.NET 应用程序中时的 HttpClient 证书不起作用
- java - 我应该使用哪个 OOP 范式?
- python - PySpark 聚合和复杂模式
- nginx - nginx proxy_pass 到烧瓶工作,而 /static 找不到 jquery
- vue.js - data 属性已经被声明为 prop
- mysql - 在 my.cnf 中添加 innobuffer 池大小后重新启动 MySQL 5.7 时出错
- flask - 烧瓶 Python URL 堆叠
- php - 在刀片模板中的 html 布局的 head 标签中设置 scripts/rel