简体   繁体   English

Chrome 扩展程序的 Javascript 帮助

[英]Javascript help for a Chrome extension

First of all, I am not a programmer.首先,我不是程序员。 I do not know Javascript at all.我根本不懂 Javascript。 I'm trying to create a Chrome extension that modifies my browser tab's title, by taking a specific string on a webpage.我正在尝试创建一个 Chrome 扩展程序,通过在网页上获取特定字符串来修改我的浏览器选项卡的标题。 I know how to create Chrome extensions (just barely) and I just need to modify the Javascript to do what I want (which I do not know how).我知道如何创建 Chrome 扩展程序(几乎没有),我只需要修改 Javascript 来做我想做的事(我不知道如何做)。

I found the following script online and am trying to modify it but can't figure out how to get it working.我在网上找到了以下脚本,正在尝试修改它,但不知道如何让它工作。 Here is the script:这是脚本:

https://pastebin.com/dY1LSdjT https://pastebin.com/dY1LSdjT

// Fire this event any time the mouse is moving.  Sucks for performance, but it's a better experience

document.addEventListener("mousemove", function() { document.addEventListener("mousemove", function() {

// This will get us the banner
let bannerTextElements = document.getElementsByClassName("dijitReset dijitInputField dijitInputContainer");

 console.log(bannerTextElements);
 console.log(bannerTextElements[0]);

if (bannerTextElements[0]) {

     console.log("ok!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     console.log(bannerTextElements[0].innerHTML);

    // This will get us the text
    let bannerTextLine = bannerTextElements[0].getElementsByClassName("dijitReset dijitInputInner");

     console.log(bannerTextLine);

document.title = bannerTextLine[0].innerHTML


}

}, false); }, 错误的);

And here's the website viewed in developer mode.这是在开发人员模式下查看的网站。 I would like to get the text "blahhhh" and use that for the title of my browser tab.我想获取文本“blahhhh”并将其用作浏览器选项卡的标题。

在此处输入图片说明

在此处输入图片说明

Below is a different webpage and the difference here is that the "div class" names will change.下面是一个不同的网页,这里的区别在于“div 类”名称会改变。 The numbers in those class will change to different numbers, but the structure and the overall name remains the same.这些类中的数字将更改为不同的数字,但结构和总体名称保持不变。 For example, "ms-Button-label label- 549 " may change to "ms-Button-label label- 640 ".例如,“ms-Button-label label- 549 ”可能会更改为“ms-Button-label label- 640 ”。 This happens whenever you refresh the page.每当您刷新页面时都会发生这种情况。 I found that the "viewport" id name doesn't change though, so I think if we can use that as a reference and then just look at the nested div classes and reference them and extract the value (in this case Crystal Mountain).我发现“viewport”id 名称并没有改变,所以我认为我们是否可以将其用作参考,然后查看嵌套的 div 类并引用它们并提取值(在本例中为 Crystal Mountain)。

在此处输入图片说明

Fixing your code修复你的代码

You got like 95% of everything you need.你得到了你需要的一切的 95%。
The one thing you are missing right now is that the fact that an input element does not posses an "innerHTML" (That is why it isn't closed like this: <input>renderedText</input> ).您现在缺少的一件事是input元素不具有“innerHTML”这一事实(这就是为什么它没有像这样关闭: <input>renderedText</input> )。 Instead you want to check for it's value:相反,您想检查它的值:

document.title = bannerTextLine[0].value;

Some Improvements:一些改进:

Search by ID按 ID 搜索
You have a well defined input which can be more easily obtained by looking it up using the id:您有一个定义明确的输入,通过使用 id 查找它可以更容易地获得:

let bannerTextLine = document.getElementById('lanDeviceIndex_searchBox');

End result:最终结果:

const bannerTextLine = document.getElementById('lanDeviceIndex_searchBox');

bannerTextLine.addEventListener("blur", function() {
    if (bannerTextLine != null) {
        document.title = bannerTextLine.value;
    }
});

This is the entire JS code.这是整个JS代码。 Note that I changed the event to blur which activates when the form is left, this should be optimal for your case.请注意,我将事件更改为模糊,当表单离开时激活,这应该最适合您的情况。

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

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