首页 > 解决方案 > 选中文本进入右键

问题描述

是否有另一种方法来突出显示文本(保留换行符)?

var newProspect = function(string){
    console.log(string.selectionText);
 };

chrome.contextMenus.create({
 title: "New Prospect",
 contexts:["all"],
 onclick: newProspect
});

标签: javascriptgoogle-chrome-extension

解决方案


var newProspect = function(string){
  chrome.tabs.executeScript(
    {
      code: "window.getSelection().toString(); ",
    },
    function (selection) {
      var selectionText = selection[0]
    }
  );
};

chrome.contextMenus.create({
  title: "New Prospect",
  contexts: ["selection"],
  onclick: newProspect,
});

推荐阅读