java 脚本在 popup.js 中不起作用
java script doesn't work in popup.js
我正在尝试构建一个 google 扩展,它从 linkedin 个人资料中获取数据,例如姓名、教育背景、技能。我有三个文件 popup.js popup.html 和 manifest.json。我的问题是 运行 我的 js 代码在 chrome 的 javascript 控制台中,它工作得很好,但是当把它放在我的 popup.js 文件中时,它似乎不起作用.任何帮助或指导将不胜感激。
这是 popup.js
document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {
chrome.tabs.query({active:true,currentWindow:true}, function(tab) {
//name
var name=$("#name").text();
alert (name);
//current-position
var current-positionn=$("#headline").text();
alert(current-position);
//phone number
var phone=$("#phone-view").text();
alert (phone);
//email
var address=$("#email").text();
alert (address);
//compétences
var sk=$('span.endorse-item-name-text').text() ; alert(sk);
//expériences
var exp=$('span.new-miniprofile-container').text() ; alert(exp);
});
}, false);
}, false);
popup.html
<html>
<head>
<title>Linkedin web scrapper</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Linkedin web scrapper</h1>
<button id="checkPage">Add Profile</button>
</body>
</html>
{
"manifest_version": 2,
"name": "Linkedin web scrapper",
"description": "This extension will fetch a linkedIn profile data",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"homepage_url": "https://www.linkedin.com/",
"permissions": [
"tabs" ,"activeTab", "https://www.linkedin.com/in/*"
]
}
我通过添加 background.js 来放置获取数据然后将数据发送到 popup.js 的代码解决了这个问题:
这是BACKGROUND.JS
var nom=$("#name").text();
var current_position=$("#headline").text();
var phone=$("#phone-view").text();
var link=$("a.view-public-profile").text();
console.log("success");
// send data to server
window.open("http:// fetchinglocalhost:8080? name="+nom+"&position="+current_position+"&phone="+phone+"&link="+link+"");
至于popup.js
document.addEventListener('DOMContentLoaded', function()
{
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {
//executing script in the background.js
chrome.tabs.executeScript(null, {file: "background.js"});
}, false);
}, false);
我正在尝试构建一个 google 扩展,它从 linkedin 个人资料中获取数据,例如姓名、教育背景、技能。我有三个文件 popup.js popup.html 和 manifest.json。我的问题是 运行 我的 js 代码在 chrome 的 javascript 控制台中,它工作得很好,但是当把它放在我的 popup.js 文件中时,它似乎不起作用.任何帮助或指导将不胜感激。 这是 popup.js
document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {
chrome.tabs.query({active:true,currentWindow:true}, function(tab) {
//name
var name=$("#name").text();
alert (name);
//current-position
var current-positionn=$("#headline").text();
alert(current-position);
//phone number
var phone=$("#phone-view").text();
alert (phone);
//email
var address=$("#email").text();
alert (address);
//compétences
var sk=$('span.endorse-item-name-text').text() ; alert(sk);
//expériences
var exp=$('span.new-miniprofile-container').text() ; alert(exp);
});
}, false);
}, false);
popup.html
<html>
<head>
<title>Linkedin web scrapper</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Linkedin web scrapper</h1>
<button id="checkPage">Add Profile</button>
</body>
</html>
{
"manifest_version": 2,
"name": "Linkedin web scrapper",
"description": "This extension will fetch a linkedIn profile data",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"homepage_url": "https://www.linkedin.com/",
"permissions": [
"tabs" ,"activeTab", "https://www.linkedin.com/in/*"
]
}
我通过添加 background.js 来放置获取数据然后将数据发送到 popup.js 的代码解决了这个问题:
这是BACKGROUND.JS
var nom=$("#name").text();
var current_position=$("#headline").text();
var phone=$("#phone-view").text();
var link=$("a.view-public-profile").text();
console.log("success");
// send data to server
window.open("http:// fetchinglocalhost:8080? name="+nom+"&position="+current_position+"&phone="+phone+"&link="+link+"");
至于popup.js
document.addEventListener('DOMContentLoaded', function()
{
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {
//executing script in the background.js
chrome.tabs.executeScript(null, {file: "background.js"});
}, false);
}, false);