如何从 'window.getSelection' 获取字符串以及如何使用 'window.open' 修复打开新标签页的问题?
How to get string from 'window.getSelection' and how to fix opening new tab by using 'window.open'?
我正在为 Firefox 创建扩展(我使用 ver.65),它可以在 Filmweb 网站(相当于 IMDB)上搜索电影的标题。通过使用任何网站上的选择并结合 Filmweb 的搜索端地址,然后在新选项卡上转到该地址,就会发生这种情况。
我尝试使用 document.getSelection 而不是 window.getSelection 但它没有用。
filmwebExt.js
const contextMenuItem = {
id: "search-on-Filmweb",
title: "Search on Filmweb",
contexts: ["selection"]
};
function getSelectionText() {
console.log('window.getSelection: ',window.getSelection());
var text = "true";
if (window.getSelection()) {
text = window.getSelection().toString();
console.log(text); //empty in debbuging console
} else if (document.selection && document.selection.type !== "Control") {
text = document.selection.createRange().text;
}
console.log(text); //empty in debbuging console
return text;
}
console.log('second window.getSelection: ',window.getSelection());
browser.contextMenus.create(contextMenuItem);
browser.contextMenus.onClicked.addListener(function (info) {
const selectedText = getSelectionText();
const url = 'https://www.filmweb.pl/search?q=';
const fullUrlAddress = url + selectedText;
if (info.menuItemId === "search-on-Filmweb") {
console.log('comparison: ',info.menuItemId === "search-on-Filmweb");
console.log("selectedText ",selectedText," fullUrlAddress ",fullUrlAddress);
window.open(fullUrlAddress, '_blank');
}
});
manifest.json
{
"manifest_version": 2,
"name": "Filmweb_Search",
"version": "1.0",
"description": "Adds Filmweb search option in context menu",
"applications": {
"gecko": {
"id": "wisznu@gmail.com"
}
},
"background": {
"scripts": [
"filmwebExt.js"
]
},
"icons": {
"48": "icons/Filmweb_icon48x48.png",
"96": "icons/Filmweb_icon96x96.png"
},
"content_scripts": [
{
"matches": [
"*://*/*"
],
"js": [
"filmwebExt.js"
]
}
],
"permissions": [
"tabs",
"activeTab",
"<all_urls>",
"contextMenus"
]
}
目前,上下文菜单项正确显示在上下文菜单中,但调试控制台显示 window.getSelection() returns 中的空值object 和 window.getSelection().toString()
的空字符串
如果 Firefox Add-On 的基础结构仍然与几年前相似,这里的问题是您无法从上下文菜单所在的进程访问文档的选择。
我相信正是出于这个原因才添加了 info
对象,以便您可以在代码为 运行 的过程中获得所需的信息。
对象 info
有一个名为 selectionText
的 属性,这就是您必须使用的对象。
打开新标签时,最好使用标签 API。
总而言之,您的 filmwebExt.js 的文件如下所示:
const contextMenuItem = {
id: "search-on-Filmweb",
title: "Search on Filmweb",
contexts: ["selection"]
};
browser.contextMenus.create(contextMenuItem);
browser.contextMenus.onClicked.addListener(info => {
if (info.menuItemId === "search-on-Filmweb") {
const url = "https://www.filmweb.pl/search?q=" + info.selectionText;
browser.tabs.create({ url });
});
我正在为 Firefox 创建扩展(我使用 ver.65),它可以在 Filmweb 网站(相当于 IMDB)上搜索电影的标题。通过使用任何网站上的选择并结合 Filmweb 的搜索端地址,然后在新选项卡上转到该地址,就会发生这种情况。
我尝试使用 document.getSelection 而不是 window.getSelection 但它没有用。
filmwebExt.js
const contextMenuItem = {
id: "search-on-Filmweb",
title: "Search on Filmweb",
contexts: ["selection"]
};
function getSelectionText() {
console.log('window.getSelection: ',window.getSelection());
var text = "true";
if (window.getSelection()) {
text = window.getSelection().toString();
console.log(text); //empty in debbuging console
} else if (document.selection && document.selection.type !== "Control") {
text = document.selection.createRange().text;
}
console.log(text); //empty in debbuging console
return text;
}
console.log('second window.getSelection: ',window.getSelection());
browser.contextMenus.create(contextMenuItem);
browser.contextMenus.onClicked.addListener(function (info) {
const selectedText = getSelectionText();
const url = 'https://www.filmweb.pl/search?q=';
const fullUrlAddress = url + selectedText;
if (info.menuItemId === "search-on-Filmweb") {
console.log('comparison: ',info.menuItemId === "search-on-Filmweb");
console.log("selectedText ",selectedText," fullUrlAddress ",fullUrlAddress);
window.open(fullUrlAddress, '_blank');
}
});
manifest.json
{
"manifest_version": 2,
"name": "Filmweb_Search",
"version": "1.0",
"description": "Adds Filmweb search option in context menu",
"applications": {
"gecko": {
"id": "wisznu@gmail.com"
}
},
"background": {
"scripts": [
"filmwebExt.js"
]
},
"icons": {
"48": "icons/Filmweb_icon48x48.png",
"96": "icons/Filmweb_icon96x96.png"
},
"content_scripts": [
{
"matches": [
"*://*/*"
],
"js": [
"filmwebExt.js"
]
}
],
"permissions": [
"tabs",
"activeTab",
"<all_urls>",
"contextMenus"
]
}
目前,上下文菜单项正确显示在上下文菜单中,但调试控制台显示 window.getSelection() returns 中的空值object 和 window.getSelection().toString()
的空字符串如果 Firefox Add-On 的基础结构仍然与几年前相似,这里的问题是您无法从上下文菜单所在的进程访问文档的选择。
我相信正是出于这个原因才添加了 info
对象,以便您可以在代码为 运行 的过程中获得所需的信息。
对象 info
有一个名为 selectionText
的 属性,这就是您必须使用的对象。
打开新标签时,最好使用标签 API。
总而言之,您的 filmwebExt.js 的文件如下所示:
const contextMenuItem = {
id: "search-on-Filmweb",
title: "Search on Filmweb",
contexts: ["selection"]
};
browser.contextMenus.create(contextMenuItem);
browser.contextMenus.onClicked.addListener(info => {
if (info.menuItemId === "search-on-Filmweb") {
const url = "https://www.filmweb.pl/search?q=" + info.selectionText;
browser.tabs.create({ url });
});