我尝试了几种方法来实现这一目标,包括添加permissions
。
这是我的Manifest.json:
{
"manifest_version": 2,
"name": "name",
"version": "0.8",
"permissions": [
"http://example.com/",
"https://example.com/"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.min.js", "content.js"],
"run_at": "document_end"
}
],
"background": {
"page": "background.html"
},
"browser_action": {
"default_icon": "icon.png",
"default_popup" : "Popup.html"
},
"permissions": [
"tabs"
],
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'"
}
popup.js:
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Get the current URL.
*
* @param {function(string)} callback - called when the URL of the current tab
* is found.
*/
function getCurrentTabUrl(callback) {
// Query filter to be passed to chrome.tabs.query - see
// https://developer.chrome.com/extensions/tabs#method-query
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
// chrome.tabs.query invokes the callback with a list of tabs that match the
// query. When the popup is opened, there is certainly a window and at least
// one tab, so we can safely assume that |tabs| is a non-empty array.
// A window can only have one active tab at a time, so the array consists of
// exactly one tab.
tab = tabs[0];
// A tab is a plain object that provides information about the tab.
// See https://developer.chrome.com/extensions/tabs#type-Tab
url = tab.url;
fixedURL = url;
fixedURL = url.substring(1);
// Open the page up.
// tab.url is only available if the "activeTab" permission is declared.
// If you want to see the URL of other tabs (e.g. after removing active:true
// from |queryInfo|), then the "tabs" permission is required to see their
// "url" properties.
//console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
// Most methods of the Chrome extension APIs are asynchronous. This means that
// you CANNOT do something like this:
//
// var url;
// chrome.tabs.query(queryInfo, function(tabs) {
// url = tabs[0].url;
// });
// alert(url); // Shows "undefined", because chrome.tabs.query is async.
}
/**
* @param {string} searchTerm - Search term for Google Image search.
* @param {function(string,number,number)} callback - Called when an image has
* been found. The callback gets the URL, width and height of the image.
* @param {function(string)} errorCallback - Called when the image is not found.
* The callback gets a string that describes the failure reason.
*/
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("main").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://localhost:8000/extension/?url="+fixedURL,true);
xmlhttp.send();
}
document.addEventListener('DOMContentLoaded', function() {
getCurrentTabUrl(function(url) {
loadXMLDoc();
//console.log(url);
}, function(errorMessage) {
});
});
点击扩展程序后得到的答案的相关部分:
$(document).ready(function(){
$("#scannedinfo").click(function(event){
event.preventDefault();
email = $('#scannedinfo-email').val();
emailval = validateEmail(email);
if(emailval != false){
$('.scannedinfo-error').text('');
$.get("https://example.com/ajaxpage",
{
email: $('#scannedinfo-email').val(),
message: $('textarea').val()
},
function(data,status){
//do stuff
}
});
} else{
//do stuff
}
});
});
当我打开我在浏览器中进行AJAX调用的URL时,它工作正常。当我检查扩展时似乎没有发生任何事情。没有错误或任何东西。 问题可能是什么? 有什么想法吗?
提前致谢!