English 中文(简体)
为什么由奥塞罗尔而不是以pl的形式行事?
原标题:Why is .js code working by console but not in form of a plug in?

I am trying to remove an element For this purpose I am trying to make small plug in in .js I have tailored this to this one specific google form I want to use it at, and for some reason I am haveing more issues in docs.google.com than I have in other websites

// content.js
const element = document.getElementsByClassName("xxxxxx");
element[0].remove(); 

xxx” 是持有电子邮件显示器的组群,I ve在这里做了改动,如果能够把电子邮件带上<>。

When I paste the code in the console manually it works as I want it , but if I run it by plug in it gives me an error code: "Uncaught TypeError: Cannot read Properties of undefined (reading remove )"

Edit: Upon request the manifest.json and the timer, I bet theres also a better way to do it

//manifest.json
{
  "manifest_version": 3,
  "name": "Auto Delete xxxxxx Elements",
  "version": "1.0",
  "description": "A Chrome extension to automatically delete elements containing the class  xxxxxx  on doc.google.com.",
  "permissions": ["activeTab"],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["*://docs.google.com/*"],
      "js": ["content.js"]
    }
  ]
}
// background.js
chrome.runtime.onInstalled.addListener(() => {
    setInterval(() => {
      chrome.tabs.query({ url: "*://docs.google.com/*" }, tabs => {
        tabs.forEach(tab => {
          chrome.scripting.executeScript({
            target: { tabId: tab.id },
            files: [ content.js ]
          });
        });
      });
    }, 1000); // 1000 milliseconds = 1 second
  });
  
问题回答

Looks like element[0] is undefined when you re trying to access it s remove() method. The plugin might be executing before the element is loaded onto the page.

你可以修改原始代码,等待上页,然后再要求取消通过DOMContentLoaded事件或窗口删除该元素的职能。 页: 1

具体如下:

document.addEventListener( DOMContentLoaded , function() {
    const element = document.getElementsByClassName("xxxxxx");
    if (element.length > 0) {
        element[0].remove(); 
    } else {
        console.error("Element with class  xxxxxx  not found.");
    }
});

So here it waits for the DOMContentLoaded event before calling remove(). It also checks if the element with class "xxxxxx" exists before trying to remove it.

Edit:尝试这样做是为了背景。 j)

chrome.runtime.onInstalled.addListener(() => {
    chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
        if (changeInfo.status ===  complete  && tab.url.startsWith( https://docs.google.com/ )) {
            chrome.scripting.executeScript({
                target: { tabId: tabId },
                files: [ content.js ]
            });
        }
    });
});

我并不真的说是熟练的,但你会因为所有nes子而忘记这一错误。

const element = document.getElementsByClassName("xxxxxx");
element[0].innerHTML = "";
element[0].remove();

Also, make sure the website is fully loaded before your plugin executes the code.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.