首页 技术 正文
技术 2022年11月22日
0 收藏 384 点赞 4,876 浏览 1483 个字

1. chrome 的 console 中不能添加本地文件

2. 下面的代码是在亚马逊的商品页面上添加一个 image, 点击之后触发 alert 函数. 其中 cBoxInner 是人工寻找到的标签

/*
* locate insertion place
*/
insertPlace = document.getElementsByClassName("cBoxInner")[2]/*
* create a button
*/myImage = document.createElement("image")
myImage.src = "https://img.zhankr.net/ejr1vmm1oew303360.gif/200px-Rotating_earth_%28large%29.gif"
myImage.onclick = function() {alert("Image is clicked")}
/*
*insert a button
*/
insertPlace.insertBefore(myImage)

3. using callback

JS 中有些函数是 asychronous 的, 比如

//THIS CODE DOESN'T WORK
var tab = chrome.tabs.query({'active': true}); //WRONG!!!
chrome.tabs.update(tab.id, {url:newUrl});
someOtherFunction();

这种风格的代码在 C++/JAVA 中一般都是行的通的, 但在 JS 中, query 函数是 asychronous, 这意味着 query will return without its work finished

为了应对这个问题, JS 有了回调函数的说法, 回调函数的 syntax 是

chrome.tabs.query(object queryInfo, function callback)

对上一行代码, 可以写成

//THIS CODE WORKS
chrome.tabs.query({'active': true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: newUrl});
});
someOtherFunction();

4. chrome.tabs.executeScript

syntax 为

chrome.tabs.exectuteScript (integer, InjectDetails details, function callback)

5. findElementById

返回对应的组件, 假如没有找到的话返回 NULL

6. findElementByClassName 返回的是一个数组, 可以通过 数组的下标获取所需

insertPlace = document.getElementsByClassName("cBoxInner")[2]

7. chrome 获取当前 tabId

function doInCurrentTab(tabCallback) {
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) { tabCallback(tabArray[0]); }
);
}
var activeTabId;
doInCurrentTab( function(tab){ activeTabId = tab.id } );

8. backgroud.js 中的代码只会执行一次, 即使设置 persistent = true — 这个实际上没啥用

要是想没打开一个网页或者刷新网页时就会触发动作, 那么就应该插入监听器, Listener

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857