简体   繁体   English

如何在 JavaScript 中将带有 css 的弹出窗口添加到现有网站(例如 google.com)?

[英]How to add a popup with css to an existing website (such as google.com) in JavaScript?

I am new to JavaScript.我是 JavaScript 的新手。 I have a task to write a js script that will be injected to google.com using Chrome DevTools and run on top of it.我有一项任务是编写一个 js 脚本,该脚本将使用 Chrome DevTools 注入到 google.com 并在其上运行。 The script needs to add certain popups on mouseover action - so if I hover over certain elements of the page (such as the Google logo), a certain popup will be shown.该脚本需要在鼠标悬停操作时添加某些弹出窗口 - 因此,如果我将鼠标悬停在页面的某些元素(例如 Google 徽标)上,则会显示某个弹出窗口。 The popups all have css stylings.弹出窗口都有 css 样式。

So far, I have managed to create alerts on mouseover action using EventListener (on google.com).到目前为止,我已经设法使用 EventListener(在 google.com 上)创建有关鼠标悬停操作的警报。 And I have managed to create custom popups with css on my own website.我已经设法在我自己的网站上使用 css 创建自定义弹出窗口。 However, I'm having serious trouble combining both.但是,我在将两者结合起来时遇到了严重的麻烦。

The problem is essentially: in my own custom website, I put the css bit under "style" tag, and the js script itself under "script" tag.问题本质上是:在我自己的自定义网站中,我将 css 位放在“style”标签下,将 js 脚本本身放在“script”标签下。 The script than uses the css properties of the popup to create it.该脚本使用弹出窗口的 css 属性来创建它。 However, in Chrome DevTools I'm only able to inject the actual js script (by copy-pasting it the console), and not the css bit.但是,在 Chrome DevTools 中,我只能注入实际的 js 脚本(通过将其复制粘贴到控制台),而不是 css 位。

How should I get around that?我应该如何解决这个问题? Is there a way to add the css within the js, so running the script will lead to the css being added to the "style" section?有没有办法在js中添加css,所以运行脚本会导致css被添加到“style”部分? Is there a different way to inject the script in the DevTools, and separately inject the css and js?在DevTools中注入脚本,并分别注入css和js,有什么不同的方式吗? Or is there another way to solve this?或者有其他方法可以解决这个问题吗?

Thanks a lot :)非常感谢 :)

You can do this by creating and running a snippet, to create a snippet:您可以通过创建和运行代码段来创建代码段:

Open chrome-devtools打开 chrome-devtools

Create new snippet ( Ctrl + Shift + P , type show snippets , and hit Enter )创建新片段( Ctrl + Shift + P ,键入show snippets ,然后按 Enter

document.head.insertAdjacentHTML("beforeend",`<style>
  /*Write your css here, sample below*/ 
  body{
   color:red !important;        
  }
 </style>`);

// your main script can go here, note, the below code is just a sample
document.body.addEventListener("mouseover", function(){
 console.log("logged..")
})

Run the snippet ( Ctrl + Enter )运行代码段( Ctrl + Enter

You can also save and use the snippet later, to run the snippet later: Ctrl + p type !您还可以稍后保存并使用该代码段,以便稍后运行该代码段: Ctrl + p type ! and the name of your snippet .以及name of your snippetname of your snippet

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

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