Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/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 在Chrome扩展背景页中获取文本选择_Javascript_Google Chrome_Google Chrome Extension_Google Chrome Devtools - Fatal编程技术网

Javascript 在Chrome扩展背景页中获取文本选择

Javascript 在Chrome扩展背景页中获取文本选择,javascript,google-chrome,google-chrome-extension,google-chrome-devtools,Javascript,Google Chrome,Google Chrome Extension,Google Chrome Devtools,我正在开发一个chrome扩展,我希望它能够按用户获取所选文本,当我在类似的非扩展页面上运行它时,代码可以正常工作,但在我的背景页面扩展中不起作用。即使我尝试在console.log中获取选择的值,它也不会返回任何内容。这是我的javascipt文件: window.onload = myOnload; function myOnload(){ var button = document.getElementById("button"); chrome.contextMenus.crea

我正在开发一个chrome扩展,我希望它能够按用户获取所选文本,当我在类似的非扩展页面上运行它时,代码可以正常工作,但在我的背景页面扩展中不起作用。即使我尝试在console.log中获取选择的值,它也不会返回任何内容。这是我的javascipt文件:

window.onload = myOnload;
function myOnload(){
  var button = document.getElementById("button");
  chrome.contextMenus.create({
    "title":"MissLang Fix",
    "contexts":["all"],
    "onclick": myClick2 
  });

  document.getElementById("input").innerHTML = "mojtaba";
  button.onclick= function(){myClick()};
}

function myClick2(){
  var txt = '';
  if (chrome.extension.getBackgroundPage().window.getSelection)
    txt = chrome.extension.getBackgroundPage().window.getSelection();
  else if (chrome.extension.getBackgroundPage().document.getSelection)
    txt = chrome.extension.getBackgroundPage().document.getSelection();
  else if (chrome.extension.getBackgroundPage().document.selection)
    txt = chrome.extension.getBackgroundPage().document.selection.createRange().text;
  console.log("this is the console message");
  console.log("txt: " + txt);
}    
function myClick(){
  var myString = document.getElementById("input").value;
  for(var i=0;i<myString.length;i++){
    myString = myString.replace("q","ض");
    myString = myString.replace("w","ص");
  }
  document.getElementById("output").innerHTML = myString;
}

可能是重复的。看,我看到了。一旦我添加getRangeAt,我的代码就停止工作。你能提供更多关于你到底在寻找什么的信息吗?
{
    "name": "MissLang Fix extension",
    "manifest_version": 2,
    "version": "1.0",
    "description": "fixing english to farsi typings.",
    "icons":{
        "16":"icon_16.png",
        "128":"icon_128.png"
    },

    "background":{
        "scripts":["script.js"]
    },

    "browser_action":   {
        "default_icon": "icon.png",
        "default_popup": "missLangFix.html"
    },

    "permissions": [
        "tabs",
        "contextMenus"
    ]

}