Google chrome extension Chrome扩展开发试图使字体粗体化

Google chrome extension Chrome扩展开发试图使字体粗体化,google-chrome-extension,Google Chrome Extension,我是一个编程初学者。 我想开发一个简单的Chrome扩展,当我点击图标时,它允许对拖动的内容进行加粗 这是我的密码: 一,。manifest.json 三,。content.js 为了获得加粗文本,现代浏览器实现了execCommand功能,允许在文本上加粗、加下划线等 您只需将所有正文内容设置为可编辑,然后运行exectCommand,如下所示: function boldText() { document.getElementsByTagName("body")[0].setAttrib

我是一个编程初学者。 我想开发一个简单的Chrome扩展,当我点击图标时,它允许对拖动的内容进行加粗

这是我的密码:

一,。manifest.json

三,。content.js


为了获得加粗文本,现代浏览器实现了
execCommand
功能,允许在文本上加粗、加下划线等

您只需将所有正文内容设置为可编辑,然后运行exectCommand,如下所示:

function boldText() {
  document.getElementsByTagName("body")[0].setAttribute("contentEditable", "");
  document.execCommand("bold");
}
我建议您始终直接在控制台中测试DOM操作(在content.js中执行的操作)。(在Chrome中点击F12,然后粘贴代码) 更多信息请访问
希望有帮助。

欢迎!您是否对此代码的某个部分有特殊问题?如果是这样,请告诉我们您的线路号码和问题。谢谢,谢谢!我会试试这个:)另外,html标签可以和整个网页的contentEditable一起使用吗?如果可能的话,我认为只需声明一次contentEditable就可以轻松地修改网页。此外,还有一个问题。“ContentEditable”属性也可以删除网页内容。我只想将其设置为粗体或斜体。除了document.execCommand之外还有什么吗?我自己回答这个问题。如果在执行函数之前和之后打开和关闭designMode,则可以执行所需的document.execCommand,而不会影响其余内容。是 啊再次感谢您抽出时间:)嘿,这是个好主意。我很高兴这个答案对你有帮助。你能投赞成票吗?马克接受了这个答案,你会很有帮助的,谢谢!
chrome.browserAction.onClicked.addListener(buttonClicked);
function buttonClicked(tab) {
  let msg = {
    txt: "hello"
  }
  chrome.tabs.sendMessage(tab.id, msg);
}
chrome.runtime.onMessage.addListener(gotMessage);
function gotMessage(message, sender, sendResponse) {
  if (message.txt === "hello") {
    var selection = window.getSelection();
    alert(selection);
    boldText(selection);
  }
}

function boldText(selection) {
  alert(selection);
  selection = selection.toString().bold();
  return false;
}
function boldText() {
  document.getElementsByTagName("body")[0].setAttribute("contentEditable", "");
  document.execCommand("bold");
}