插件83:获得Ajax请求

<html><head><title>AJAX GET Example</title>
</head><body><center />
<h1>Loading a page in-between <font face='Courier New'><div>…</div></font> tags</h1>
<div id='info'>The contents of this DIV will be replaced</div>

<script type="text/JavaScript">

PIPHP_JS_GetAjaxRequest('ajaxget.php',
   'url=http://amazon.com/mobile',
   document.getElementById('info'))

function PIPHP_JS_GetAjaxRequest(url, params, target)
{
   nocache = "&nocache=" + Math.random() * 1000000
   request = new PIPHP_JS_AjaxRequest()
   
   request.onreadystatechange = function()
   {
      if (this.readyState == 4)
         if (this.status == 200)
            if (this.responseText != null)
               target.innerHTML = this.responseText
// You can remove these two alerts after debugging
            else alert("Ajax error: No data received")
         else alert( "Ajax error: " + this.statusText)
   }

   request.open("GET", url + "?" + params + nocache, true)
   request.send(null)
}

function PIPHP_JS_AjaxRequest()
{
   try
   {
      var request = new XMLHttpRequest()
   }
   catch(e1)
   {
      try
      {
         request = new ActiveXObject("Msxml2.XMLHTTP")
      }
      catch(e2)
      {
         try
         {
            request = new ActiveXObject("Microsoft.XMLHTTP")
         }
         catch(e3)
         {
            request = false
         }
      }
   }
   return request
}
</script>

ajaxget.php:

<?php // ajaxget.php

if (isset($_GET['url']))
   echo file_get_contents($_GET['url']);

?>

插件说明:

插件83接受一个网页的URL地址和传递个网页的参数,以及一个用来保存结果的文档对象模型(DOM)。他不返回任何参数,但是如果操作成功,文档对象模型将保持从Web服务器返回的结果。如果操作失败,则出现一个有利于调试的警告窗口。在生产型网站上,我们可能希望禁用这种调试。他需要以下参数:

1.URL:调用Web页面或程序。

2.params:GET参数,用"&"把各参数分开,如var1=value1&var2=value2。

3. target: DOM元素,用来保存调用返回的结果。

好的,下面是大致的步骤: 1. 创建一个新的Chrome插件项目,可以在manifest.json文件中指定需要的权限,例如拦截网络请求和访问DOM。 2. 在background.js文件中编写拦截请求的代码。可以使用chrome.webRequest.onBeforeRequest监听XMLHttpRequest和fetch请求,并在回调函数中修改响应结果。 ```javascript chrome.webRequest.onBeforeRequest.addListener( function(details) { // 拦截请求并修改响应结果 return {redirectUrl: "data:text/plain;charset=UTF-8," + encodeURIComponent("Hello, World!")}; }, {urls: ["<all_urls>"]}, ["blocking"] ); ``` 3. 在manifest.json文件中指定需要注入的内容脚本,例如可以在所有页面注入一个JS文件,以便在页面上修改响应结果。 ```json "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ] ``` 4. 在content.js文件中编写修改响应结果的代码。可以使用MutationObserver监听DOM的变化,并在回调函数中修改响应结果。 ```javascript var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { // 修改响应结果 mutation.target.textContent = "Hello, World!"; }); }); observer.observe(document, {subtree: true, characterData: true, childList: true}); ``` 5. 打包并安装插件。在Chrome浏览器中打开扩展程序页面,点击“加载已解压的扩展程序”按钮,选择插件的根目录,即可安装插件并测试拦截和修改请求的功能。 以上是一个简单的拦截和修改请求的Chrome插件的步骤,具体实现还需要根据自己的需求进行调整。希望这些信息能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值