JS一种设置Cookie然后加载页面的方法 [英] JS A Method To Set Cookie Then Load Page

查看:102
本文介绍了JS一种设置Cookie然后加载页面的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我访问的网站经常有点击更改为黑暗主题按钮。黑暗的主题更容易阅读,所以我必须在每次访问时点击链接(历史记录在关闭时清除)。



我在Firefox的本地Linux机器上;所以可以使用Javascript,HTML,Python和Bash。



我专注于书签,因为它似乎是正确的方式 - 但是,我正在做的事情就是工作。我不熟悉Javascript所以这几天都在查找和尝试示例。它可能无法作为Bookmarklet,所以也许还有另一种方式?



Bookmarklet - Set Then Go



这就是我所拥有的:

  javascript:document.cookie =theme = dark; domain = example.org; path = /;; location.href = http://www.example.org; 

如果它只是cookie,它可以工作,如果它只加载网站,它可以工作 - 但不是两者都有效。我可以通过编程方式首先访问页面,设置cookie,然后重新加载,但这也不起作用。我不想点击两个链接。 example.org是如何存储cookie以进行正常访问,我只使用cookie来持续该会话。



持久性Cookie



我无法让Firefox(IceCat)保留cookie。我将它设置为Exceptions允许,但没有任何东西保留它,除非保持cookie直到它们过期和Cookies未经检查关闭后删除。这样做可以保留所有我不想做的cookie。



永久Cookie



我'我尝试在不可变的文件中设置cookie - 但Firefox不再保留cookie了。他们在SQLite中。也许有一种方法可以在行上设置一个标志,以便它永远不会删除? User.js和prefs.js似乎不适用 - 但是不应该在prefs.js或user.js中设置cookie,为每个浏览器加载设置它,从而创建一个'永久cookie'? / p>

Bookmarklet - Go Then Click



我尝试的另一件事是打开页面,然后以编程方式单击链接到加载黑暗主题:

  javascript:location.href =http://www.example.org; document.getelementbyid ( '黑暗')点击()。 

该ID是已知的,并且是无效锚的形式(无href)。该网站的JavaScript似乎正在侦听点击事件。以下是他们的内容:

  $('#Dark')。click(function(){
document。 cookie ='theme = dark; domain = .example.org; path = /';
window.location.reload(true);
});

所以不应该像设置环境变量然后加载网站一样简单 - 也许JS没有没有环境变量?



注入并忘掉它



我读到有可能注入风格进入页面的头部,所以我试图注入:

 < script> document.getelementbyid('Dark')。单击();< /脚本> 

通过Bookmarklet看起来应该可以正常工作,因为点击将在本地环境中。我永远无法让它注入,但我再也不知道Javascript。



最后的度假胜地



如果上面没有任何可能的话,是否可以使用书签来设置本地.html文件,该文件为给定域设置cookie然后重定向到该站点?最后一个不喜欢的沟渠解决方案是实际的脚本(.sh / .py)?






我不喜欢我想分享网站,我不想在退出时不清楚历史。也不是因为你可能在想什么。 :)



我在这里是因为我想要一个Javascript解决方案(书签),但我不擅长Javascript - 所以上面的所有例子都可能有效通过调整,我只是做错了。



感谢所有人的帮助!!如果你可以解决它 - 你能解释为什么我的工作不起作用吗?

解决方案

我最终想出了一个cookie初始条目只能由域设置,作为针对XSS的度量。所以我的问题的答案是不可能的。



最简单的解决方法是使用加载项后跟userContent.css。



在加载页面之前设置主题首选项(cookie)会很不错 - 但我可以看到为什么规则已经到位..



如果使用JS,则需要相同的点击次数才能访问该网站并单击更改为黑暗主题(打开网站[第一次点击],设置cookie并重新加载[第二次点击])。 / p>

I visit a site often that has a "click to change to dark theme" button. The dark theme is much easier to read, so I have to click the link every visit (history cleared on close).

I'm on a local Linux box with Firefox; so Javascript, HTML, Python and Bash are available.

I'm focused on a Bookmarklet as it seems like the right way - however, nothing I'm doing is working. I'm not versed in Javascript so it's been days looking up and trying examples. It may not be possible as a Bookmarklet, so perhaps there is another way around?

Bookmarklet - Set Then Go

Here's what I have:

javascript:document.cookie="theme=dark; domain=example.org; path=/;"; location.href="http://www.example.org";

It works if it's only the cookie, and it works if it only loads the site - but not both. I'm ok with programmatically going to the page first, setting the cookie, then reloading but that doesn't work either. I'm not wanting to click two links. "example.org" is how the cookie is stored for a normal visit, and I'm ok with the cookie only lasting for that session.

Persistent Cookie

I can't get Firefox (IceCat) to keep the cookie. I set it under Exceptions to allow, but nothing keeps it unless both "Keep cookies until they expire" and "Cookies" is unchecked for removal after close. Doing that keeps all cookies from everywhere which I'm not wanting to do.

Permanent Cookie

I'd tried setting the cookie in an immutable file - but Firefox doesn't keep cookies there anymore. They are in SQLite. Maybe there's a way to set a flag on the row so it's never removed? User.js and prefs.js don't seem to apply - but shouldn't it be possible to set the cookie in prefs.js or user.js that would set it for each browser load thereby creating a 'permanent cookie'?

Bookmarklet - Go Then Click

Another thing I tried was to open the page, then click the link programmatically to load the dark theme:

javascript:location.href="http://www.example.org";document.getelementbyid('Dark').click();

The ID is known and in the form of a non-valid anchor (no href). The site's javascript seems to be listening for a click event. Here is what they have:

$('#Dark').click(function(){
    document.cookie='theme=dark; domain=.example.org; path=/';
    window.location.reload(true);
});

So shouldn't it be as simple as setting an environment variable then loading the site - maybe JS doesn't have environment vars?

Inject It And Forget It

I read that it was possible to inject style into the head of the page, so I tried to inject:

 <script>document.getelementbyid('Dark').click();</script>

via the Bookmarklet which seems like it should work as the click would be in a local context. I could never get it to inject, but again, I don't know Javascript.

Last Resort

If nothing above is possible, could it be done with bookmark to a local .html file that sets the cookie for the given domain then redirects to the site? A last ditch solution that is not preferred would be an actual script (.sh/.py)?


I don't want to share the site, and I don't want to not clear history on exit. Neither are due to what you're probably thinking. :)

I'm here because I'm wanting a Javascript solution (a bookmark), but I'm not good at Javascript - so all the examples above might work with tweaking, I'm just doing it wrong.

Thanks all for any help!! If you can fix it - could you also explain why what I had doesn't work?

解决方案

I eventually figured out a cookie's initial entry can only be set by the domain, as a measure against XSS. So the answer to my question is it's not possible.

The simplest work around is to use an Add-On followed by userContent.css.

It would of been nice to set the theme preference (cookie) before loading the page - but I can see why the rule's in place..

It takes the same amount of clicks to go to the site and click "Change To Dark Theme" as it would if JS was used (open the site [1st click], set the cookie and reload [2nd click]).

这篇关于JS一种设置Cookie然后加载页面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆