使用油猴脚本在网页中插入按键

  • 测试环境 chrome 浏览器 + 暴力猴插件 + www.baidu.com

要求

  • 懂javascript 与 jquery

code

// ==UserScript==
// @name test
// @namespace AceScript Scripts
// @match https://www.baidu.com/
// @grant none
// ==/UserScript==

(function () {
	'use strict';
	console.log('我的脚本加载了');
	var button = document.createElement("button"); //创建一个input对象(提示框按钮)
	button.id = "id001";
	button.textContent = "百度二下";
	button.style.width = "60px";
	button.style.height = "20px";
	button.style.align = "center";

	//绑定按键点击功能
	button.onclick = function (){
		console.log('点击了按键');
        //为所欲为 功能实现处
		alert("你好");
		return;
	};
  
    var x = document.getElementsByClassName('btn_wr s_btn_wr bg')[0];
    //在浏览器控制台可以查看所有函数,ctrl+shift+I 调出控制台,在Console窗口进行实验测试
	x.appendChild(button);
    
    //var y = document.getElementById('s_btn_wr');
    //y.appendChild(button);
})();
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐