javascript - jQuery 代码不会在 chrome 扩展的内容脚本中运行
问题描述
我只是想知道是否有人对为什么此代码不会在我的 chrome 扩展的内容脚本中运行有任何意见。扩展程序设置正确,因为我在内容脚本中有可以正常工作的代码,由于某种原因,该代码根本无法工作。当我将它粘贴到代码需要运行的页面的控制台时,该代码就会运行。
代码:
function ucall(user) {
window.open('test.com/userid=' + user, 'popup', 'width=600', 'height=600')
};
console.log("READY?")
var comments = document.getElementsByClassName('group-comments')[0]
console.log(comments)
$(comments).on('hover', '.wall-comment', function () {
var user = $(this).find('a')[1].href;
user = user.replace(/[^\d]/g, ''); console.log(user);
console.log("do you hover bro?")
$(this).css({ 'background-color': 'yellow' });
$(this).click(function () {
ucall(user)
})
});
我为什么它可能不起作用的理论是我认为在变量“comments”中声明的 div 没有及时加载以执行代码。所以我试着做$(comments).ready(function(){//code here})
;那仍然没有用。我很困惑。此外,我的内容脚本中的所有代码都在一个
$(document).ready(function(){// all content script code in here})
解决方案
在这里我添加了代码请检查这个..
function ucall(user) {
window.open('test.com/userid=' + user, 'popup', 'width=600', 'height=600')
};
console.log("READY?")
// Here I have selected the first instance of the class using jQuery
var $comments = $('.group-comments').first();
console.log(comments)
//Used jQuery Instance directly.
$comments.on('hover', '.wall-comment', function () {
var user = $(this).find('a')[1].href;
user = user.replace(/[^\d]/g, ''); console.log(user);
console.log("do you hover bro?")
$(this).css({ 'background-color': 'yellow' });
$(this).click(function () {
ucall(user)
})
});
推荐阅读
- reactjs - 重新渲染 NextJS 路由(React 组件)
- python - 使用 pandas.to_datetime() 推断日期时如何检查月份顺序是否不明确?
- javascript - 如何防止导航栏抖动?
- sql-server - 从 SQL Server 中的 xml 将不同的元素放入一列
- kademi - 如何确定配置文件是否已经在 Journy Decision 节点中设置了密码
- css - GRID CSS — 我怎么能不定义网格行端?
- mysql - 从 4 行 1 列中获取数据并分成 9 列的正确 MySQL 方法是什么?
- google-apps-script - 如何在最初加载表单时使用 Google Apps 脚本更改现有的 Google 表单元素
- javascript - Android Webview - 页面加载完成后使用 Javascript 更改元素
- mongodb - 使用 mongo-kafka 作为接收器连接器,如何将主题记录的值字段映射到另一个值?