简体   繁体   English

HTML注入javascript代码

[英]Html injection into javascript code

first of all i should say that it's my first day of learning java script. 首先,我应该说这是我学习Java脚本的第一天。

Basically i'm trying to create chrome extention which will add dummy button after search bar button in "google.com". 基本上我正在尝试创建chrome扩展,它将在“ google.com”中的搜索栏按钮后添加虚拟按钮。

So far i've written this code: 到目前为止,我已经编写了以下代码:

var x = document.getElementsByName("btnK");
if (typeof x[0] === 'undefined' || x[0] === null) {

} else {
    parent = x[0].parentElement;
    button = document.createElement("button");
    parent.appendChild(button);
}

As you can see i'm getting google's search button, then getting parent of it and add button as last to it. 如您所见,我正在获取google的搜索按钮,然后使其成为父项,最后添加按钮。 The issue here is that after i'm creating this button it can't be modified in any way. 这里的问题是,在我创建此按钮后,无法以任何方式对其进行修改。

As i understand i can't change style or text of newly created element, only elements that was already defined when form was loading. 据我了解,我无法更改新创建元素的样式或文本,仅可以更改在加载表单时已定义的元素。 So, knowing this information i want to ask(my question is below) 所以,知道我想问的信息(我的问题在下面)

Question: Is there any way to insert button, created in html code into my script? 问题:有什么方法可以将以html代码创建的按钮插入到脚本中?

Note: I understand it might be easy and trivial question, and i'm sorry that i couldn't find clear and right answer just by googling, so far i'm kind of confused with javascript 注意:我知道这可能是一个简单而琐碎的问题,很抱歉我无法通过谷歌搜索找到清晰,正确的答案,到目前为止,我对javascript感到困惑

Edit: After comment i need to clearify one thing. 编辑:评论后,我需要澄清一件事。 I can't use any of code to the button after createElement , it has no effect, so basically i need to create it with style already deined somehow 我不能在createElement之后对按钮使用任何代码,它没有任何作用,所以基本上我需要使用已经以某种方式定义的样式来创建它

Edits: Thanks all for the suggestion in comments, here what i've tried so far: 编辑:谢谢大家在评论中的建议,这是我到目前为止尝试过的:

1st approach: 第一种方法:

As was suggested, i just straight copy button "search", change it a bit and readded to parent. 正如建议的那样,我只是直接复制按钮“搜索”,将其更改一点并读入父对象。 I hoped to see three buttons now, but it's just redrawed search button in new position, here's the code: 我希望现在能看到三个按钮,但是它只是在新位置重新绘制了搜索按钮,下面是代码:

x[0].value = "test"
parent.appendChild(x[0]);

2nd approach: 第二种方法:

Create button, then get it and change, also not working, here's the code: 创建按钮,然后获取它并进行更改,这也不起作用,下面是代码:

button.id = "dummyId"
parent.appendChild(button);
var styledButton = document.getElementById("dummyId");
styledButton.value = "test";

It changed the value if i'm looking at this button in "inspect element", but visually it's empty 如果我在“检查元素”中查看此按钮,它会更改值,但在视觉上它是空的

Guys, sorry, it was my stupidity. 伙计们,对不起,这是我的愚蠢。 It just that value field doesn't change text !!! 只是value字段不会更改文本! And also i needed to create input, not value 而且我需要创造输入,而不是价值

So, final soultion: 因此,最后的灵魂:

    input = document.createElement("input");
    input.type = "submit"
    input.value = "test"
    parent.appendChild(input);

Sorry again 再次抱歉

When you create the button, you can add, add text, classes, event listeners etc. 创建按钮时,可以添加,添加文本,类,事件监听器等。

button = document.createElement("button");
button.id = 'my-new-button';
button.textContent = 'Click me';
button.classList.add('my-button');
button.addEventListener('click', function() {
    alert('clickie');
});

Demo: https://jsfiddle.net/ubmb6p3f/1/ 演示: https : //jsfiddle.net/ubmb6p3f/1/

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

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