Jai*_*oks 0 html javascript url google-chrome-extension
我正在尝试使用下面的代码制作一个简单的chrome扩展,输入文本和一个按钮,点击一下按钮,我想拉出一个特定的URL.我在编写代码时遇到了麻烦.我是JavaScript的新手.
<!doctype html>
<html>
<head>
<title>Title Name</title>
<style>
body {
min-width: 357px;
overflow-x: hidden;
}
img {
margin: 5px;
border: 2px solid black;
vertical-align: middle;
width: 75px;
height: 75px;
}
</style>
</head>
<body>
<input type="text" name="enter" class="enter" value="67" id="lolz" />
<button type="button" id="the_button">LookUp Site ID</button>
<script src="popup.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
var VanillaRunOnDomReady = function() {
document.getElementById('the_button').addEventListener('click', myFunction);
function myFunction() {
var siteid = document.getElementById('lolz').value
//window.location = "https://www.websiteimusing.com/siteid" + siteid;
chrome.tabs.create({ url: "https://www.websiteimusing.com/siteid" + siteid});
}
}
Run Code Online (Sandbox Code Playgroud)
}
{
"manifest_version": 2,
"name": "ID URL opener",
"description": "Enter ID and it will pull up the correct URL",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["tabs"]
}
Run Code Online (Sandbox Code Playgroud)
它没有填充错误,只是在按钮点击时没有实际加载URL,任何想法?
好吧,它实际上非常简单.你只需要添加一个background.js文件.
这是扩展的流程:
你完成了,你愿意我写这段代码吗?
编辑:守则
最好的部分是,现在您不需要根据最新的chrome扩展文档将消息传递给background.js,可以从弹出窗口访问选项卡权限.以下代码将弹出一个输入字符串,并将用户发送到一个新的选项卡,在该选项卡中搜索输入字符串在谷歌上.大多数代码都是自解释的,Lemme知道如果你无法解决这个问题,我会发给你crx + source
manifest.json的:
{
"name" : "Simple Search",
"version" : "0.1",
"manifest_version" : 2,
"description" : "Simple Search Plugin",
"browser_action": {
"default_icon": {
"19": "images/icon_48.png"
},
"default_title": "Simple Search",
"default_popup": "popup.html"
},
"permissions" :
[
"tabs"
],
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}
Run Code Online (Sandbox Code Playgroud)
popup.html
<html>
<head>
<style>
body{
font-family:"lucida grande",tahoma,verdana,arial,sans-serif;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="popup.js"></script>
</head>
<body style="width:300px;">
<center>
<h1>Simple Search</h1>
<input type="text" id="q"></input>
<input type="button" id="s" value="search">
</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
popup.js
$(function() {
$('#s').click(function() {
var url = "https://www.google.com/search?q=" + $('#q').val();
chrome.tabs.create({url: url});
});
});
document.addEventListener('DOMContentLoaded');
Run Code Online (Sandbox Code Playgroud)