根据我的知识,不可能直接通过获取tab.url(仅在popup.html中可能)并且进行消息传递也需要打开popup.html.反正有没有绕过这个并从background.html获取当前页面的网址?
我最好的镜头是消息传递,我在background.html中使用了这段代码
var bg = chrome.extension.getPopupPage();
var myURL = bg.myURL;
Run Code Online (Sandbox Code Playgroud)
然后在popup.html我有:
chrome.tabs.getSelected(null, function(tab) {
var myURL = tab.url;
})
Run Code Online (Sandbox Code Playgroud)
无论如何,上述根本不起作用.有人知道这样做的方法而不必实际打开弹出窗口吗?
我正在编写这个javascript,它将在几个其他域上使用,这些域调用一个php脚本(仅在我的域上)来返回一个数组.我正在使用xmlhttp,它在我的域上测试时效果很好,但是一旦从单独的域放置或调用javascript它就会完全中断.有人知道如何跨域提出此请求吗?
注意:我必须执行一个奇怪的小黑客,允许我进行两次单独的调用,并确保它们在处理之前都返回.无论如何,这在我的域名上每次都能完美运行.
这是调用我的PHP代码的javascript文件
function getUrls(){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
xmlhttp2 = new XMLHttpRequest();
}
else {
// code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// code for IE5 and IE6
xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
parsedJSONurls = JSON.parse(xmlhttp.responseText);
xmlhttp2.open("GET", "http://mydomain.com/connect.php?q=companies", true);
xmlhttp2.send();
}
}
xmlhttp2.onreadystatechange = function(){
if (xmlhttp2.readyState == 4 && xmlhttp2.status == …Run Code Online (Sandbox Code Playgroud) javascript php xmlhttprequest cross-domain same-origin-policy
我有一个约50个项目的数组,在javascript中它通过这个数组来检查一个条件,如果满足条件,它会为要检查的复选框设置属性.它很顺利,但每次只更改数组中的最后一项.这是代码的一部分.
for (i = 0; i < urlArray.length; i++) {
var newImg = new Image();
var coName = companyArray[i];
newImg.onload = function(){
if (condition is met){
nStar++;
document.getElementById(coName).setAttribute("checked", "checked");
document.getElementById(coName).checked = true;
} else {
nStar++;
document.getElementById(coName).setAttribute("checked", "checked");
document.getElementById(coName).checked = true;
document.getElementById(coName).setAttribute("disabled", "disabled");
document.getElementById(coName).disabled = true;
}
};
Run Code Online (Sandbox Code Playgroud)
因此,您可以看到是否满足条件,它仍将更改属性,但我收到的错误是它只更改数组中的最后一项.有任何想法吗?
到目前为止没有运气试验.我需要运行一个php函数来检查一个标题(从我的理解不可能通过js),但页面需要是一个.js,因为它正在被其他页面访问,并且它被要求保持这样.
如果我不能直接在页面中运行PHP,我将不会动摇,但总是有其他方式.我只想看看是否有一个简单的方法.
编辑:这都是在服务器上运行.对不起,我没有说清楚.(这不是一个noob错误)