最简单的Chrome扩展插件V3版本
单位升级了OA办公系统,有一个必填项,通常写“同意”,然而系统设计不合理,需要打字或点好几次鼠标才行。自己写个chrome extension来填写吧。
在任意目录中,建立2个文件:manifest.json和content.js
1、manifest.json
{ "version": "0.0.1", "name": "Test", "description": "Test", "host_permissions": ["*://*/*"], "content_scripts": [ { "js": ["content.js"], "matches": ["*://192.168.1.2/*"] } ], "manifest_version": 3 }
192.168.1.2/*匹配OA办公系统网址,其它没什么可解释的
2、content.js
//延时1秒,保证页面动态加载的元素都加载完 setTimeout("fireContentLoadedEvent ()", 1000) function fireContentLoadedEvent () { console.log (window.location.href); try{ el_1=document.querySelector("#div > div.wf > span.si"); el_1.innerText="同意"; ifr = document.querySelector("#cks > iframe"); ifr.contentDocument.querySelector("body > p").innerText="同意"; }catch(e){} };
本来想在DOMContentLoaded事件后执行,总有问题,怀疑是因为页面内容是延迟动态加载,而且还有iframe,于是干脆暴力等1秒后再操作DOM,效果不错
小技巧之一:在chrome中按Ctrl + Shit +J ,在 元素 中找到要操作的网页元素,在元素上 鼠标右键-复制-复制js路径,得到类似document.querySelector(" span")的元素,然后粘贴到控制台中先测试一下,如:document.querySelector(" span").click()
小技巧之二:对于iframe一直头痛,但js操作也不难,如上面的代码,先用 document.querySelector取到ifame元素 , 再用.contentDocument.querySelector取iframe中的元素
最后就是,点chrome右上方的三个点-更多工具-扩展程序 ,打开 开发者模式 加载已解压的扩展程序 找到上述两个文件所在目录加载。加载后可以关闭 开发者模式
然后工作时就可以稍微省点事了。
浙公网安备 33010602011771号