繁体   English   中英

尝试弄一个示例扩展,但是我的JS似乎不起作用

[英]Trying to mess around with a sample extension but my JS doesn't seem to work

我正在弄乱Page Redder示例扩展名,但是我不知道为什么我的JS无法使用。 单击扩展名时,原始扩展名将背景色更改为红色。 我正在尝试使其变为将其CSS选择器中找到的某些单词改为红色,在我的情况下,选择器为“ var.d”,这是JS :(注释底部的JS是原始码)

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can    be
// found in the LICENSE file.

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
// No tabs or host permissions needed!
console.log('Turning ' + tab.url + ' red!');
chrome.tabs.executeScript({
code: 'document.querySelectorAll("var.d");'

for (var i = 0; i < xx.length; i++) {
xx[i].style.color="red";
}

 });
});


// 'document.body.style.backgroundColor="red";

您的代码无法正常工作,因为for循环未包含在“代码”属性中,并且未声明xx变量。

试试下面的代码。 只需确保“ var.d”选择器确实存在,否则您将不会获得任何结果。

chrome.browserAction.onClicked.addListener(function(tab) {
  // No tabs or host permissions needed!
  console.log('Turning ' + tab.url + ' red!');
  chrome.tabs.executeScript({
    code: 'var xx = document.querySelectorAll("var.d"); for (var i = 0; i < xx.length; i++) {xx[i].style.color="red";}'});
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM