javascript - InnoSetup - 有没有办法为 Internet Explorer 手动创建 cookie?

标签 javascript windows internet-explorer cookies inno-setup

基本上 IE 记得网站 www.stackoverflow.com 使用 JavaScript cookie,但是是否可以在 InnoSetup 中代表 stackoverflow.com 手动创建相同的 cookie?

Javascript cookie:

function setCookie(cname,cvalue,exdays) {
  var d = new Date();
  d.setTime(d.getTime()+(exdays*24*60*60*1000));
  var expires = "expires="+d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++)  {
    var c = ca[i].trim();
    if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
  return "";
}

function checkCookie(user) {
  var user=getCookie("username");
  if (user!="") {
    alert("Welcome again " + user);
  } else  {
    user='bandwidth - set to off for example';
    setCookie("username",user,30);  
  }
}

注意:因为无法从 IE 检测我的插件是否安装。我突然意识到我必须处理 cookies 。但我的插件必须在首次安装时创建该 cookie,而不是 IE。

编辑:引用

http://msdn.microsoft.com/en-us/library/windows/desktop/aa385107%28v=vs.85%29.aspx

最佳答案

要创建与指定 URL 关联的 cookie,您可以使用 InternetSetCookie功能。要检索指定 URL 的 cookie,您可以使用 InternetGetCookie功能。下面是他们的翻译,其中包含一个示例,展示了如何创建和读取 cookie(真正的代码实现,涉及错误消息,或者我会一直关注的包装函数;以此代码为例,展示如何使用这些 API 函数):

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

const
  ERROR_INSUFFICIENT_BUFFER = 122;
  ERROR_NO_MORE_ITEMS = 259;

function InternetSetCookie(lpszUrl: string; lpszCookieName: string;
  lpszCookieData: string): BOOL;
  external 'InternetSetCookie{#AW}@wininet.dll stdcall';
function InternetGetCookie(lpszUrl: string; lpszCookieName: string;
  lpszCookieData: string; var lpdwSize: DWORD): BOOL;
  external 'InternetGetCookie{#AW}@wininet.dll stdcall';

function TryCreateCookie(const URL, Name, Data: string): Boolean;
begin
  // try to create a specified cookie
  Result := InternetSetCookie(URL, Name, Data);
  // if the function call failed, we can optionally report the reason why
  if not Result then
    MsgBox('Cookie creation failed!' + #13#10 +
      SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;

function TryRetrieveCookie(const URL, Name: string; out Data: string): Boolean;
var
  S: string;
  BufferSize: DWORD;
begin
  // initialize function result
  Result := False;
  // initialize buffer size to 0 to query needed buffer size 
  BufferSize := 0;
  // first call is to determine whether there's a requested cookie, or if so,
  // to retrieve the needed buffer size
  if not InternetGetCookie(URL, Name, #0, BufferSize) then
  begin
    // the function failed as expected, so let's inspect the reason
    case DLLGetLastError of
      // if the reason for failure was the insufficient buffer, it means that
      // there's a cookie matching the request and that we have just received
      // the required buffer size
      ERROR_INSUFFICIENT_BUFFER:
      begin
        // initialize buffer size by the previously returned size
        SetLength(S, BufferSize div SizeOf(Char));
        BufferSize := Length(S);
        // and call the function again, now with the initialized buffer; this
        // time it should succeed; if it is so, then...
        if InternetGetCookie(URL, Name, S, BufferSize) then
        begin
          // everything went fine, so let's return success state and assign a
          // retrieved value to the output parameter
          Result := True;
          Data := S;
        end
        else
          // the second function call failed; that should not happen...
          MsgBox('Cookie retrieval failed!' + #13#10 +
            SysErrorMessage(DLLGetLastError), mbError, MB_OK);
      end;
      // this code is returned when there's no cookie found
      ERROR_NO_MORE_ITEMS:
      begin
        // no cookie matching the criteria was found; the return value of this
        // function has already been initialized to False but it's upon you to
        // react on this fact somehow, if needed
      end;
    else
      // the first function call failed for unexpected reason
      MsgBox('Cookie search failed!' + #13#10 +
        SysErrorMessage(DLLGetLastError), mbError, MB_OK);
    end;
  end;    
end;

procedure InitializeWizard;
var
  CookieData: string;
begin  
  // try to create cookie
  TryCreateCookie('http://example.com', 'MyCookie',
    'TestData=1234; expires=Wed,31-Dec-2014 23:59:59 GMT');
  // try to retrieve cookie
  if TryRetrieveCookie('http://example.com', 'MyCookie', CookieData) then
    MsgBox(CookieData, mbInformation, MB_OK);
end;

关于javascript - InnoSetup - 有没有办法为 Internet Explorer 手动创建 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675548/

相关文章:

c++ - 在 Windows 上与 USB HID 设备进行通信的最佳 USB 库是什么?

css - 旋转关键帧在 50% 和 75% 之间逆时针旋转

javascript - 如何将数组项的单个值存储到父索引中?

windows - 在 Windows 上的双显示器上使用 NetBeans 8

javascript - OAuth : How to hide API Secret Key from javascript

c++ - Windows 如何知道用什么程序打开文件?

javascript - 如何从 IE 和 Chrome 浏览器中的 showmodaldialog 窗口返回数组对象?

javascript - 为什么我应该测试 MSXML2.XmlHttp 和 Microsoft.XmlHttp 版本无关的 ProgID?

javascript - AngularJS 中未选择默认选项下拉列表

javascript - 当我按转义键时如何按空格键