Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未填充新的搜索URL_Javascript_Url_Google Chrome Extension_Tabs - Fatal编程技术网

Javascript 未填充新的搜索URL

Javascript 未填充新的搜索URL,javascript,url,google-chrome-extension,tabs,Javascript,Url,Google Chrome Extension,Tabs,我正在构建一个扩展,允许您通过contextMenu搜索google新闻中的选定文本,但选定文本不会显示在我的查询中。我错过了什么 background.js manifest.json 谢谢:)我让它工作了。必须使用消息传递并添加JSON.stringify(info.selectionText);如果有人遇到同样的问题,我会这样做: background.js contentscript.js 背景脚本在其单独的隐藏背景页中运行,因此getSelection()不会帮助您,因为它在那里是空的

我正在构建一个扩展,允许您通过contextMenu搜索google新闻中的选定文本,但选定文本不会显示在我的查询中。我错过了什么

background.js manifest.json
谢谢:)

我让它工作了。必须使用消息传递并添加JSON.stringify(info.selectionText);如果有人遇到同样的问题,我会这样做:

background.js contentscript.js
背景脚本在其单独的隐藏背景页中运行,因此getSelection()不会帮助您,因为它在那里是空的。onClicked的侦听器已在
信息中收到所选文本。selectionText
,请参阅。谢谢。我使用content.js和后台之间的消息发送和接收响应。它是双向工作的,但现在的问题是它搜索[object object],当我使用JSON.stringify(selectedText)时,它向我提供有关所选文本的所有信息,而不是字符串本身
函数sendServiceRequest(selection){var searchQuery=encodeURIComponent(selection.toString())var serviceCall=https://news.google.com/search?q=“+searchQuery;chrome.tabs.create({url:serviceCall});
我的观点是,您根本不需要消息和内容脚本,只需直接使用info.selectionText。。。。
//creates a context menu When selection is made
contextMenus.createSelectText = chrome.contextMenus.create(
    {"title":"Search for '%s'",
    "contexts": ["selection"],
    "id": "selectedtext"
    });


var searchURL = 'https://news.google.com/search?q=' + window.getSelection().toString();

function searchGoogleForString(url) {
    chrome.tabs.create({url: searchURL});
}
chrome.contextMenus.onClicked.addListener(onRequest);
function onRequest(info, tab) {
    console.log("User clicked Context Menu Search for " + "selectedtext");
    searchGoogleForString(tab["url"]);
}
{
  "name": "Find in Da News",
  "description": "Find a similar article",
  "version": "0.1.1",
  "permissions": [
    "contextMenus",
    "tabs",
    "activeTab"
  ],
  "background": {
    "scripts": ["scripts/background.js"],
    "persistent": true
  },
  "manifest_version": 2,
  "icons": {
    "48": "squatch.png",
    "128": "squatch.png"
  },
  "browser_action": {
    "default_icon": "squatch.png",
    "default_popup": "popup.html"
  }
}

var contextMenus = {};
//creates a context menu WHEN selection is made
contextMenus.createSelectText = 
    chrome.contextMenus.create(
        {"title":"Search for '%s'",
        "contexts": ["selection"],

        },
        function (){
        if(chrome.runtime.lastError){
            console.error(chrome.runtime.lastError.message);
        }
    }
);


chrome.contextMenus.onClicked.addListener(sendServiceRequest);

chrome.extension.onRequest.addListener(function(tab){
    chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function(response){
        sendServiceRequest(response.data);
    });
});

function sendServiceRequest(info, selection) {

    var serviceCall = 'https://news.google.com/search?q=' + JSON.stringify(info.selectionText);
    chrome.tabs.create({url:serviceCall});
    console.log(serviceCall);
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    if(request.method == "getSelection")
        else
    sendResponse({});
})